1. object.__lt__(self, other):触发<
    2. object.__le__(self, other):触发<=
    3. object.__eq__(self, other):触发==
    4. object.__ne__(self, other):触发!=
    5. object.__gt__(self, other):触发>
    6. object.__ge__(self, other):触发>=
    1. class Compare:
    2. def __init__(self, a: int, b: int):
    3. print("这是一个比较对象,初始化init方法")
    4. self.a = a
    5. self.b = b
    6. def __eq__(self, other):
    7. """自定义等于"""
    8. if type(self) == type(other) and self.__dict__ == other.__dict__:
    9. return True
    10. else:
    11. return False
    12. def __lt__(self, other):
    13. """自定义小于"""
    14. if self.a < other.a and self.b < other.b:
    15. return True
    16. else:
    17. return False
    18. def __str__(self):
    19. print(f'打印对象信息{self.__name__}')
    20. obj1 = Compare(2, 3)
    21. obj2 = Compare(2, 3)
    22. print(obj1.__eq__(obj2))
    23. print(obj1.__lt__(obj2))
    24. """
    25. 这是一个比较对象,初始化init方法
    26. 这是一个比较对象,初始化init方法
    27. True
    28. False
    29. """