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

标签 python python-3.x

我读过这个(来自 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/

相关文章:

python - Tensorflow Keras Input层没有添加_keras_shape

python - Python C API 中的多重继承

python - 如何找到矩阵最后一行的最小值?

python - 如何逐步在 Python Selenium 中向下滚动

python - 在nodejs pyshell中导入已安装的python包时出错

python - 导入错误 : No module named 'tensorflow.core'

python - 您如何使用另一个包含列表的列表来排序包含列表的列表?

python - 没有轮询的基于选择的套接字循环

python - 如何使用 python 中的循环将键和值分配给字典?

python - Django:类型错误:实例(模型对象)之间不支持 '<'