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

标签 python hash set python-internals magic-methods

给定:

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__

关于python - 使用 __hash__ 函数设置包含用户定义的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42061019/

相关文章:

python - 集合文字给出了与集合函数调用不同的结果

sql-server-2008 - 有没有办法让 SQL Server 自动对 nvarchar 字段的哈希值进行选择?

c# - .NET C# 中的集合操作

php - 从另一个 PHP 类设置 protected 变量的正确方法

Java设置多线程处理

python - 为什么 Flask SQL Alchemy 允许保存 None 主键?

python - 数据集不适合 LSTM 训练的内存

javascript - 为什么域在谷歌分析中被散列?

python - 如何让别人用我的.py文件来测试呢?

python - 从txt文件中读取和写入数字