object.__lt__(self, other):触发<
object.__le__(self, other):触发<=
object.__eq__(self, other):触发==
object.__ne__(self, other):触发!=
object.__gt__(self, other):触发>
object.__ge__(self, other):触发>=
class Compare:
def __init__(self, a: int, b: int):
print("这是一个比较对象,初始化init方法")
self.a = a
self.b = b
def __eq__(self, other):
"""自定义等于"""
if type(self) == type(other) and self.__dict__ == other.__dict__:
return True
else:
return False
def __lt__(self, other):
"""自定义小于"""
if self.a < other.a and self.b < other.b:
return True
else:
return False
def __str__(self):
print(f'打印对象信息{self.__name__}')
obj1 = Compare(2, 3)
obj2 = Compare(2, 3)
print(obj1.__eq__(obj2))
print(obj1.__lt__(obj2))
"""
这是一个比较对象,初始化init方法
这是一个比较对象,初始化init方法
True
False
"""