python用is判断,底层是比较什么的
is:比较 对象身份(内存地址)。
==:比较 对象内容(由 eq 决定),与地址无关。
解释器先把 a == b 翻译成
a.eq(b) 的调用(如果没有就退回到 id(a) == id(b))
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
p1 = Point(1, 2)
p2 = Point(1, 2)
print(p1 == p2) # True,内容一样即可,地址不同也没关系
只有没写 eq 的默认对象,才会退化成比较地址(等价于 is)