“hashable"是什么意思?在 Python 中是什么意思? [英] What does "hashable" mean in Python?
问题描述
我尝试在互联网上搜索但找不到 hashable 的含义.
当他们说对象是 hashable
或 hashable 对象
是什么意思?
来自 Python 词汇表
一个>:
<块引用>如果一个对象的哈希值在其生命周期内永远不会改变(它需要一个 __hash__()
方法),并且可以与其他对象进行比较(它需要一个 __eq__()
或 __cmp__()
方法).比较相等的可散列对象必须具有相同的散列值.
哈希能力使对象可用作字典键和集合成员,因为这些数据结构在内部使用哈希值.
Python 的所有不可变内置对象都是可散列的,而没有可变容器(例如列表或字典)是可散列的.默认情况下,作为用户定义类实例的对象是可散列的;它们都比较不相等,它们的哈希值是它们的id()
.
I tried searching internet but could not find the meaning of hashable.
When they say objects are hashable
or hashable objects
what does it mean?
From the Python glossary:
An object is hashable if it has a hash value which never changes during its lifetime (it needs a
__hash__()
method), and can be compared to other objects (it needs an__eq__()
or__cmp__()
method). Hashable objects which compare equal must have the same hash value.Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their
id()
.
这篇关于“hashable"是什么意思?在 Python 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!