简体   繁体   English

使用pickle.dumps哈希可变对象

[英]Using pickle.dumps to hash mutable objects

I understand why putting mutable objects in a dictionary is dangerous. 我了解为什么将可变对象放入字典很危险。 However, converting all lists/sets to tuples/frozensets is expensive; 但是,将所有列表/集合转换为元组/ frozensets是昂贵的; 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. 因此,有时值得直接散列可变对象,并采取适当的预防措施以确保所讨论的对象永远不会被修改。

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.dumps用作哈希函数是否存在任何缺点-无论是从性能,碰撞还是其他方面。

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或pprint或repr用于哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM