我读过这个(来自 here ):
User-defined classes have
__eq__()
and__hash__()
methods by default; with them, all objects compare unequal (except with themselves) andx.__hash__()
returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y).
我想知道默认情况下 __eq__()
方法是否定义如下:
def __eq__(self, other):
return hash(self) == hash(other)
最佳答案
不,更像是:
def __eq__(self, other)
return self is other
您不能使用 hash()
,因为不同的对象可能具有相同的哈希值。
关于python - 默认情况下 __eq__() 方法里面有什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57899891/