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__()
method). Hashable objects which compare equal must have the same hash value.
一个对象的哈希性是指在生命周期内不可改变(内置__hash__()方法
),并且可以和其他对象比较(内置__eq__()方法
),当这两个对象相等的时候具有相同的哈希值。
Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
具有哈希性的对象才可作为字典的key和set的成员。
All of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not. Objects which are instances of user-defined classes are hashable by default. They all compare unequal (except with themselves), and their hash value is derived from their id()
.
所有不可改变的内置对象都是哈希的,可变的集合不具有哈希性(list、dictionary)用户自定义类的实例默认具有哈希性,而他们的哈希值重他们id性质中来。