英文:
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内部的工作方式,这并不是一个神秘之事,但您可能会发现这种行为令人困扰:
>>> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论