Python中的__hash__和__eq__方法之间的一些使用问题

 代码版本:3.6.3;文档版本:3.6.6

 object.__hash__(self)

Called by built-in function hash() and for operations on members of hashed collections including setfrozenset, and dict.

 __hash__()方法会被上述四种情况调用。

If a class does not define an __eq__() method it should not define a __hash__()operation either; if it defines __eq__() but not __hash__(), its instances will not be usable as items in hashable collections. If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

如果自定义类没定义__eq__()方法,那也不应该定义__hash__()方法。 

如果定义了__eq__()方法没有定义__hash__()方法,那么它无法作为哈希集合的元素使用(这个hashable collections值得是set、frozenset和dict)。这其实是因为重写__eq__()方法后会默认把__hash__赋为None(文档后面有说),像list一样。 

class A:
    def __eq__(self, other):
        pass


a = A()

print(a.__hash__)  # None
hash(a)
"""TypeError: unhashable type: 'A'"""

还专门说明:如果定义可变对象的类实现了__eq__()方法,就不要再实现__hash__()方法,否则这个对象的hash值发生变化会导致被放在错误的哈希桶中。这个可以用字典试一下,你的键值不在是一一对应的,只要能让这两个方法返回一致的对象都能改动那个本不属于自己的值,这篇文章的第五个例子就是这种情况。

User-defined classes have __eq__() and __hash__() methods by default; with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)

用户定义的类默认都有__eq__()和__hash__()方法,这是从object继承的,如果你不重写任何一个,那么对这个类的两个实例x,y来说,x is y ,x == y , hash(x) == hash(y)会同时成立/不成立,即只有在x就是y的时候成立。

A class that overrides __eq__() and does not define __hash__() will have its __hash__()implicitly set to None. When the __hash__() method of a class is None, instances of the class will raise an appropriate TypeError when a program attempts to retrieve their hash value, and will also be correctly identified as unhashable when checking isinstance(obj, collections.abc.Hashable).

 重写了__eq__()方法的类会隐式的把__hash__赋为None。当获取实例的哈希值即用到了__hash__()方法时(只有上文提到的四种情况会用到这个方法)就会抛出TypeError错误,上文例子演示过了。并且isinstance判断类型也能正确判断。

# 直接安装不成功  pip install collections2 才行
# collections2==0.3.0  A set of improved data types inspired by the standard library's collections module.
import collections


class A:
    def __eq__(self, other):
        pass


class B:
    pass


a = A()
b = B()

print(isinstance(a, collections.abc.Hashable))
print(isinstance(b, collections.abc.Hashable))
"""
False
True
"""

If a class that overrides __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ =<ParentClass>.__hash__.

If a class that does not override __eq__() wishes to suppress hash support, it should include __hash__ = None in the class definition. A class which defines its own __hash__() that explicitly raises a TypeError would be incorrectly identified as hashable by an isinstance(obj, collections.abc.Hashable) call.

 如果一个类重写了__eq__()方法还需要能使用父类的__hash__()方法(上文已说默认情况下是被赋值为None了),那就需要明确的说明一下:例class A;如果一个类没有重写__eq__()方法而又需要让__hash__()失效,那就要明确的赋值为None,像list、set等的源码那样。如果你重写了一个会抛出异常的__hash__()方法,虽然使用时会抛出异常,但是类型判断还是会判断为是可哈希的,这是要注意的:例class B。

import collections


class A:
    def __eq__(self, other):
        pass
    __hash__ = object.__hash__


class B:
    def __hash__(self):
        raise TypeError('There is an error!')


a = A()
b = B()

print(isinstance(a, collections.abc.Hashable))
print(isinstance(b, collections.abc.Hashable))
hash(b)
"""
True
True
...line 12, in __hash__...
TypeError: There is an error!
"""

文档位置:3.6.6 object.__hash__

猜你喜欢

转载自blog.csdn.net/lnotime/article/details/81194962