问题描述
我明白为什么将可变对象放入字典是危险的.然而,将所有列表/集合转换为元组/冻结集是很昂贵的;对于许多类型,根本没有容易获得的不可变版本.因此,有时可能值得直接对可变对象进行哈希处理,并采取适当的预防措施以确保相关对象永远不会被修改.
I understand why putting mutable objects in a dictionary is dangerous. However, converting all lists/sets to tuples/frozensets is expensive; and for many types, there's no easily available immutable versions at all. So, sometimes it may be worth hashing mutable objects directly, with the appropriate precautions to ensure the objects in question are never modified.
在我开始为可变对象实施非常复杂自定义哈希函数之前,我想检查一下是否有任何缺点使用 pickle.dumps
作为散列函数 - 无论是在性能、冲突还是其他方面.
Before I start implementing the very complicated custom hash functions for mutable objects, I wanted to check if there's any disadvantage to using pickle.dumps
as the hash function - either in terms of performance or collisions or anything else.
推荐答案
pickle 的输出不能保证是规范的,原因与 dict 和 set order 的不确定性类似.不要使用 pickle 或 pprint 或 repr 进行散列.
The output from pickle is not guaranteed to be canonical for similar reasons to dict and set order being non-deterministic. Don't use pickle or pprint or repr for hashing.
这篇关于使用 pickle.dumps 散列可变对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!