__gt__
大于
__lt__
小于
__eq__
等于python
运算符远不止这些!!!!!能够再object类中进行查看!!
当咱们在使用某个符号时,python解释器都会为这个符号定义一个含义,同时调用对应的处理函数, 当咱们须要自定义对象的比较规则时,就可在子类中覆盖 大于 等于 等一系列方法....函数
本来自定义对象没法直接使用大于小于来进行比较 ,咱们可自定义运算符来实现,让自定义对象也支持比较运算符code
class Student(object): def __init__(self,name,height,age): self.name = name self.height = height self.age = age def __gt__(self, other): # print(self) # print(other) # print("__gt__") return self.height > other.height def __lt__(self, other): return self.height < other.height def __eq__(self, other): if self.name == other.name and self.age == other.age and self.height == other.height: return True return False stu1 = Student("jack",180,28) stu2 = Student("jack",180,28) # print(stu1 < stu2) print(stu1 == stu2)
上述代码中,other指的是另外一个参与比较的对象,
大于和小于只要实现一个便可,符号若是不一样 解释器会自动交换两个对象的位置对象