Skip to main content
 首页 » 编程设计

python之使用 __hash__ 函数设置包含用户定义的类

2024年10月01日2tintown

给定:

class T: 
    def __hash__(self): 
        return 1234 
 
t1 = T() 
t2 = T() 
 
my_set = { t1 } 

我希望以下内容打印 True:

print t2 in my_set 

这不是应该打印 True 吗,因为 t1t2 具有相同的哈希值。如何使 setin 运算符使用给定的哈希函数?

请您参考如下方法:

您需要定义一个 __eq__ 方法,因为只有相同的实例 a is b 或等于 a == b (除了具有相同的 hash) 将被 setdict 识别为相等:

class T: 
    def __hash__(self): 
        return 1234 
    def __eq__(self, other): 
        return True 
 
t1 = T() 
t2 = T() 
 
my_set = { t1 } 
 
print(t2 in my_set)  # True 

data model on __hash__ (和 the same documentation page for Python 2 )对此进行了解释:

__hash__

Called by built-in function hash() and for operations on members of hashed collections including set, frozenset, and dict. __hash__() should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and hashing the tuple.

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).

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).

(强调我的)

注意:在 Python 2 中,您还可以实现 __cmp__ 方法而不是 __eq__