pickle文件可复制吗?

huangapple go评论73阅读模式
英文:

Are pickle files reproducible?

问题

我想要对一些 pickle 文件进行哈希以进行验证,但我想知道 Python 的 pickle 是否总是针对相同的输入产生相同的输出,至少在协议版本内是这样吗?我想知道操作系统是否会产生差异?你有参考资料吗?

英文:

I would like to hash some pickle files for verification but I wonder if Python's pickle always produces the same output for the same input, at least within a protocol version? I wonder if the OS makes a difference? Do you have any references?

答案1

得分: 2

目前看来,pickle处理过程 总是 确定性的

此外,根据pickle内部的工作方式,这并不是一个神秘之事,但您可能会发现这种行为令人困扰:

>>> class B:
...     pass
>>>
>>> b = B()
>>> b.x = 1
>>> b.y = 2
>>>
>>> c = B()
>>> c.y = 2
>>> c.x = 1
>>> hash(pickle.dumps(b))
5326405855805501882
>>> hash(pickle.dumps(c)) 
-2711706543463941149

据记录,似乎曾尝试使用pickle进行科学复现,但该项目现在似乎已经被放弃。

英文:

It would seem that as of now, the pickle process is not always deterministic.

Also, this is not a mystery according to how pickle works internally, but you might find this behavior disturbing:

>>> class B:
...     pass
>>>
>>> b = B()
>>> b.x = 1
>>> b.y = 2
>>>
>>> c = B()
>>> c.y = 2
>>> c.x = 1
>>> hash(pickle.dumps(b))
5326405855805501882
>>> hash(pickle.dumps(c)) 
-2711706543463941149

For the record, it seems that there was an attempt at using pickle for scientific reproducibility purposes, but the project seems now abandoned.

huangapple
  • 本文由 发表于 2023年4月4日 16:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定