Loading

python property说明

参考:

1. 解释为什么及何时适合用这语法:https://blog.csdn.net/AlanGuoo/article/details/78855750

2. 代码写法示例:https://blog.csdn.net/huo_1214/article/details/79242516

 

 # _name 是表示私有变量,关于_的使用规范,可以在本博客的另一篇文章中查看
1
class Person(object): 2 def __init__(self, name="wang"): 3 self._name = name 4 5 @property 6 def name(self): 7 return self._name 8 9 @name.setter 10 def name(self, value): 11 self._name = value 12 13 @name.deleter 14 def name(self): 15 del self._name 16 17 if __name__ == "__main__": 18 p = Person() 19 print(p.name) 20 p.name = "new_Wang" 21 print(p.name) 22 del p.name 23 print(p.name) # AttributeError: 'Person' object has no attribute '_name'

 

posted @ 2020-04-14 21:22  wsongl  阅读(181)  评论(0编辑  收藏  举报