面向對象高階-補漏01比較魔法方法(__gt__等)

對象進行 == , >, < 等計較運算時候觸發python

__gt__ 爲大於code

__lt__ 爲小於對象

__eq__ 爲等於源碼

more and more , 能夠點進源碼查看it

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)
相關文章
相關標籤/搜索