默认情况下__eq __()方法中的内容

rvc*_*and 2 python python-3.x

我读到这里(从这里):

用户定义的类默认具有__eq__()__hash__()方法。使用它们,所有对象比较不相等(除了它们本身)并x.__hash__()返回适当的值,以使x == y暗示x为y和hash(x)== hash(y)。

我想知道__eq__()默认情况下该方法是否定义为:

def __eq__(self, other):
    return hash(self) == hash(other)
Run Code Online (Sandbox Code Playgroud)

Bar*_*mar 7

不,它更像是:

def __eq__(self, other)
    return self is other
Run Code Online (Sandbox Code Playgroud)

您无法使用,hash()因为不同的对象可能具有相同的哈希值。