本文介绍了使用pickle.dumps来散列可变对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白为什么在字典中放置可变对象是危险的。但是,将所有列表/集合转换为元组/元组是很昂贵的;对于很多类型来说,根本没有可用的不可变版本。因此,有时直接使用哈希可变对象是值得的,并且有适当的预防措施以确保所讨论的对象不会被修改。

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或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来散列可变对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 12:36