Python 中的 __hash__

官方文档对 可哈希的解释:hashable

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.
Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
Most of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not; immutable containers (such as tuples and frozensets) are only hashable if their elements are hashable. 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().


若对象在其生命周期内保持不变,而且能与其他对象相比较,那么这个对象是可哈希的。

通过__hash__返回一个int值,用来标记这个对象。对于类而言,如果没有实现 __eq__ 和 __hash__ 函数,那么会自动继承object.__hash__

   def __hash__(self):
        return hash(id(self))
    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return hash(id(self))==hash(id(other))
        else:
            return False   
>>> class Z:
...     def __init__(self, x):
...         self.x = x
>>> x = Z(0)
>>> y = Z(0)
>>> x == y
False 

对 __hash__ 和 __eq__ 重载:

>>> class A:
...     def __init__(self, x):
...         self.x = x
...     def __eq__(self, obj2):
...         if isinstance(obj2, self.__class__):
...             return self.__dict__ == obj2.__dict__
...         else:
...             return False
...     def __hash__(self):
...         if isinstance(int, self.x):
...             return hash(self.x)
...         elif isinstance(str, self.x):
...             hash = 0
...             for val in self.x:
...                 hash = 31 * hash + self.x - "a"
...             return hash
...         else:
...             return hash(self.x)
>>> class B:
...     def __init__(self, x):
...         self.x = x
...     def __eq__(self, obj):
...         return False
...     def __hash__(self):
...         return hash(self.x)
>>> a = A(1)
>>> b = A(1)
>>> a == b
True
>>> c = B(1)
>>> d = B(1)
>>> c == d
False

hash()函数保证了在同一个解释器进程里,相同字符串的hash一致。(如果要使用可重现可跨进程保持一致性的hash,可使用hashlib)

Reference:
https://docs.python.org/3/reference/datamodel.html#object.hash
https://fangjian0423.github.io/2016/03/12/java-Object-method/
https://www.zhihu.com/question/57526436/answer/153238652
https://blog.csdn.net/anlian523/article/details/80910808


版权声明:本文为Jiaach原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。