オブジェクト指向の高次 - マジックトラップ法(__gt__、等)の01比較

関係なく、オブジェクトを計算==、>、<、等トリガ

__gt__ 为大于

__lt__ 为小于

__eq__ 为等于

より多くの、あなたがソースコードを表示するためにクリックスルーすることができます

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)


おすすめ

転載: www.cnblogs.com/suren-apan/p/11595075.html