装饰器property
作用:
将类中的函数属性伪装成数据属性
对象访问属性时,触发方法执行,将返回值作为本次结果.
class Teacher(): def __init__(self,name, age): self.__name = name self.age = age @property # 将方法伪装成数据属性 def name(self): return self.__name @name.setter def name(self, name): if not isinstance(name, str): print('名字不是字符串') return self.__name = name @name.deleter def name(self): print('不让删') # t = Teacher('TOM', 29) # t.name # print(t.name) # t.name = 123 # >>>: 触发装饰器name.setter对应函数name(t,123) # t.name = 'zero'