判断对象是否小于另一个对象,可通过obj < other来调用。
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __lt__(self, other):
return self.x < other.x and self.y < other.y
point1 = Point(1, 2)
point2 = Point(3, 4)
print(point1 < point2) # 输出: True