gpt4 book ai didi

python - 默认情况下 __eq__() 方法里面有什么

转载 作者:行者123 更新时间:2023-11-28 20:12:31 24 4
gpt4 key购买 nike

我读过这个(来自 here ):

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__() 方法是否定义如下:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com