特性一:只读属性
class Bank_acount: @property def password(self): return "123" andy = Bank_acount() print(andy.password) # 123 andy.password = '密碼:456' # AttributeError: can't set attribute
Property 特性二:重新实现一个属性的 setter、getter 和 deleter 方法
class Bank_acount: def __init__(self): self._password = "预设密码 0000" @property def password(self): return self._password @password.setter def password(self, value): self._password = value @password.deleter def password(self): del self._password print('del complite') andy = Bank_acount() # getter print(andy.password) # 预设密码 0000 andy.password = '1234' # setter print(andy.password) # 1234 del andy.password # deleter print(andy.password) # del complite