@property 类属性不能传参数,但可以使用 self,根据其只能使用self属性,所以利用@setter赋值
class Dog(object): def __init__(self,name): self.name = name self.__food =None @property def eat(self): print("%s is eating %s "%(self.name,self.__food)) @eat.setter def eat(self,food): self.__food=food print("set to food",food) d = Dog("hh") d.eat d.eat = 'baozi' d.eat