Using pickle.dumps to hash mutable objects

走远了吗. 提交于 2019-12-29 09:32:23

问题


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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/12727571/using-pickle-dumps-to-hash-mutable-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!