Python 学习笔记property
来源:互联网 发布:网络销售平台搭建方案 编辑:程序博客网 时间:2024/06/05 18:57
python中 property的定义是这样的
fget 是一个方法获得属性的值,fset是一个方法给属性赋值,fdel是一个方法删除属性的值,典型的使用是定义一个管理属性x:就像下面这样
如果c是C的一个实例,c.x将会调用getter, c.x=value 将会调用setter 并且 del c.x 将会调用 deleterclass C(object): 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.")
下面是python的官方解释
- property([fget[,fset[,fdel[,doc]]]])
Return a property attribute for new-style classes (classes that derive fromobject).
fget is a function for getting an attribute value, likewise fset is a function for setting, andfdel a function for del’ing, an attribute. Typical use is to define a managed attributex:
class C(object): 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 anddelc.x the deleter.
If given, doc will be the docstring of the property attribute. Otherwise, the property will copyfget‘s docstring (if it exists). This makes it possible to create read-only properties easily usingproperty() as adecorator:
class Parrot(object): def __init__(self): self._voltage = 100000 @property def voltage(self): """Get the current voltage.""" return self._voltage
turns the voltage() method into a “getter” for a read-only attribute with the same name.
A property object has getter,setter, anddeleter methods usable as decorators that create a copy of the property with the corresponding accessor function set to the decorated function. This is best explained with an example:
class C(object): def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x
This code is exactly equivalent to the first example. Be sure to give the additional functions the same name as the original property (x in this case.)
The returned property also has the attributes fget,fset, andfdel corresponding to the constructor arguments.
New in version 2.2.
Changed in version 2.5: Use fget‘s docstring if no doc given.
Changed in version 2.6: The getter, setter, anddeleter attributes were added.
- python学习笔记 - @property
- python学习笔记 - @property
- python学习笔记 - @property
- Python 学习笔记property
- python学习笔记 使用@property
- Python学习笔记——Property用法
- python学习-property
- Python模块学习--property
- python学习03 --@property
- python3学习笔记:@property
- ios 学习笔记之property
- python核心编程学习笔记-2016-08-13-05-property()函数
- python学习笔记(21)--类的详解3-属性函数(@property)
- 【Python】学习笔记——-8.2、面向对象高级编程:2.使用@property
- python学习笔记2:面向对象的高级特性2 @property
- python学习——使用@property
- python -- property()/@property
- WTL 学习笔记 -- Property Sheets and Wizards
- Qt中显示复杂列表
- neutron delete net,subnet
- 判断字符串是否为空
- Shell统计代码行数
- 程序员的感悟
- Python 学习笔记property
- vim python
- spring事务管理几种方式(转)
- Unity3d运行时程序创建NGUI图集 - 转载
- Vision引擎中效果图形-雾图形介绍
- android分页查询获取系统联系人信息
- 7.1.1.1 处理记录
- 公司新问题又出现了
- 联想笔记本电脑蓝牙没有“发送文件”和“接受文件”选项的解决方法