Python学习笔记——Property用法
2017-07-21 19:40
543 查看
property使用方法有两种,写好以后发挥的作用是相同的。
两种方法运行结果都一样。只不过用法不同而已。
说白了就是懒,就是为了直接使用 t.num 这种方法操作私有属性。
写法一
class Test(object): """docstring for Test""" def __init__(self): super(Test, self).__init__() self.__num = 10 def getNum(self): return self.__num def setNum(self,newNum): self.__num = newNum num = property(getNum,setNum)
写法二(装饰器)
class Test(object): """docstring for Test""" def __init__(self): super(Test, self).__init__() self.__num = 100 @property def num(self): return self.__num @num.setter def num(self,newNum): self.__num = newNum
两种方法运行结果都一样。只不过用法不同而已。
说白了就是懒,就是为了直接使用 t.num 这种方法操作私有属性。
相关文章推荐
- 【Python学习笔记之二】浅谈Python的yield用法
- python3学习笔记:@property
- Python学习笔记 - 字典的用法
- Python学习笔记(4)Python中super的用法
- Python学习笔记:05 数组的基本用法(Tuple和List)
- PYTHON 学习笔记之 join 和 split 函数用法
- python学习笔记:range()函数用法
- Python 学习笔记property
- Python学习笔记:python中enumerate函数的用法
- 三、PYTHON 学习笔记之 join 和 split 函数用法
- Python学习笔记——print语句用法总结
- 【python学习笔记】Python中*args 和**kwargs的用法
- Python学习笔记(二)if语句的简单用法
- python学习笔记-(14)python循环中的高级用法
- python cookbook第三版学习笔记十五:property和描述
- Python学习笔记(十八)@property
- Python学习笔记(二):条件控制语句与循环语句及常用函数的用法
- Python的装饰器用法学习笔记
- Python学习笔记——部分常用/特殊用法
- Python学习:纠错笔记:详解os.removedirs(path)的正确用法