day21-双下eq方法

class Goods:
    def __init__(self,name):
        self.name = name
    def __eq__(self,other):  #self = apple1, other = apple2
        if self.name == other.name:
            return True
        else:
            return False
apple1 = Goods('apple')
apple2 = Goods('apple')
print(apple1 == apple2) #True, 双等号==触发双下__eq__方法

 

posted @ 2019-08-05 17:04  梁劲雄  阅读(133)  评论(0编辑  收藏  举报