我读到这里(从这里):
用户定义的类默认具有
__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)
不,它更像是:
def __eq__(self, other)
return self is other
Run Code Online (Sandbox Code Playgroud)
您无法使用,hash()因为不同的对象可能具有相同的哈希值。
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |