python 基础之关系运算进阶

 关系运算符

  ```python
  class Number:
        def __init__(self, num):
            self.num = num

        # 大于 >
        def __gt__(self, other):
            print('__gt__')
            return self.num > other

        # 小于 <
        def __lt__(self, other):
            print('__lt__')
            return self.num < other

        # 等于 ==, 判断是否相等,当不实现__ne__时,!=运算也会触发
        def __eq__(self, other):
            print('__eq__')
            return self.num == other

        # 大于等于 >=
        def __ge__(self, other):
            print('__ge__')
            return self.num >= other

        # 小于等于 <=
        def __le__(self, other):
            print('__le__')
            return self.num <= other

        # 不等于 !=
        def __ne__(self, other):
            print('__ne__')
            return self.num != other
          
  n = Number(20)

  print(n > 10)
  print(n < 10)
  print(n == 10)
  print(n != 10)    

 

posted @ 2018-12-21 22:33  青春叛逆者  阅读(223)  评论(0编辑  收藏  举报