python中property干什么用的?
2015-06-30 17:03
691 查看
先来段官方文档压压惊。。
property的用法是为你的类设置一个属性值。第一个参数设定获取属性值的方法,第二个参数设定设置这个属性值的方法,第三个参数设定删除这个属性值的方法,第四个参数是文档。c.x会调用第一个参数的方法,c.x = value 调用第二个方法,del c.x调用第三个方法。这样
property(fget=None, fset=None, fdel=None, doc=None)
Return a property attribute.
fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del’ing, an attribute. Typical use is to define a managed attribute x:
If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.
Return a property attribute.
fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del’ing, an attribute. Typical use is to define a managed attribute x:
class C: def __init__(self): self._x = None def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, "I'm the 'x' property.")
If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.
property的用法是为你的类设置一个属性值。第一个参数设定获取属性值的方法,第二个参数设定设置这个属性值的方法,第三个参数设定删除这个属性值的方法,第四个参数是文档。c.x会调用第一个参数的方法,c.x = value 调用第二个方法,del c.x调用第三个方法。这样
相关文章推荐
- 北京地铁月度消费总金额计算(Python版)
- python 3 过滤股票
- python读写ini配置文件方法实例分析
- Python输出中文乱码问题
- Scrapy使用过程中的中文乱码问题
- Python subprocess模块学习总结
- PYTHON无法使用Image模块
- Python OpenCV 图像相识度对比
- Python Unittest 自动化单元测试框架Demo
- Python Unittest 自动化单元测试框架Demo
- Python读写Excel 2007文件(xlsx)
- python之SocketServer传文件实例
- Python学习基础四
- python清除指定目录内所有文件中script的方法
- Python学习基础三
- Python基础学习二
- Python基础学习一
- python 编码小结
- Python ---IDEL清屏
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)