Python property,属性
參考资料
http://www.ibm.com/developerworks/library/os-pythondescriptors/
顾名思义,property用于生成一个属性。通过操作这个属性。能够映射为对某些函数的操作,类似于C#。
形式为
pvar = propery(get_func, set_func, del_fun, doc_func);
在get。set,del,doc的时候分别调用对应的函数;
尝试这样定义
>>> aa = property(lambda: "hello", lambda x: print(x))
>>> aa
<property object at 0x7fdcd9ecf9a8>
>>> aa = 1
看来property仅仅有在class中才有意义(看后文)
>>> class Test:
... aa = property(lambda self: "hello", lambda
self,x: print(x))
...
>>> t = Test()
>>> t.aa
'hello'
>>> t.aa = (1,2,2)
(1, 2, 2)
property的神奇面纱
事实上propery并非一个真正的函数,而是一个类
>>> type(property)
<class 'type'>
而普通的函数为
>>> def f():
... pass
...
>>> type(f)
<class 'function'>
property类实现了__get__, __set__, __delete__方法,这3个方法合在一起,就定义了描写叙述符规则,实现了当中不论什么一个方法的对象就叫描写叙述符(descriptor)。描写叙述符的特殊之处在于它使怎样被訪问的。比方,程序读取一个属性时。假设该属性被绑定到实现了__get__方法的对象上。那么就会调用__get__方法(返回其结果),而不是简单的返回该对象。
class Something:
... def __get__(self, instance, owner):
... print(instance)
... print(owner)
... return "__get__"
... def __set__(self, instance, name):
... print(instance)
... print(name)
... print("__set__")
>>> class Holder:
... s = Something()
...
>>> h = Holder()
>>> h.s
<__main__.Holder object at 0x7fdcd9ed8518>
<class '__main__.Holder'>
'__get__'
>>> h.s = 3
<__main__.Holder object at 0x7fdcd9ed8518>
__set__
Something的__get__ 和 __set__方法的參数instance,相应了Something对象所所绑定的对象;能够想象。Property是通过instance调用传入当中的get, set, del, doc。
须要注意的是:描写叙述符必须被赋值给类属性,而不是对象实例属性,才有效;
>>> class Something:
... pass
>>> setattr(Something, 'aa', property(lambda self: "hello", lambda self, x: print(x)))
>>> t.aa
'hello'
>>> t.aa = 3
而设置在对象实例上,则无效果:
>>> setattr(t, 'aa', property(lambda self: "hello", lambda self, x: print(x)))
>>> setattr(Something, 'aa', None)
>>> t.aa
<property object at 0x7fdcd9edf4f8>
>>> t.aa = 3
另外,还能够通过注解(Annotation)的方式定义property
class C(object):
| @property
| def x(self):
| "I am the 'x' property."
| return self._x
| @x.setter
| def x(self, value):
| self._x = value
| @x.deleter
| def x(self):
| del self._x
http://www.ibm.com/developerworks/library/os-pythondescriptors/给了一种动态创建Property的方法
class Person(object): def addProperty(self, attribute): # create local setter and getter with a particular attribute name getter = lambda self: self._getProperty(attribute) setter = lambda self, value: self._setProperty(attribute, value) # construct property attribute and add it to the class setattr(self.__class__, attribute, property(fget=getter, \ fset=setter, \ doc="Auto-generated method")) def _setProperty(self, attribute, value): print "Setting: %s = %s" %(attribute, value) setattr(self, '_' + attribute, value.title()) def _getProperty(self, attribute): print "Getting: %s" %attribute return getattr(self, '_' + attribute) |
Python property,属性的更多相关文章
- python - property 属性函数
Python中有一个被称为属性函数(property)的小概念,它可以做一些有用的事情.在这篇文章中,我们将看到如何能做以下几点: 将类方法转换为只读属性 重新实现一个属性的setter和getter ...
- Python——@property属性描述符
@property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/getter也是需要的 假设定义了一个类Cls,该类必须继承自object类,有一私 ...
- python @property 属性
在绑定属性时,如果我们直接把属性暴露出去,显然不合适,是通过getter和setter方法来实现的,还可以定义只读属性,只定义getter方法,不定义setter方法就是一个只读属性: class P ...
- python property属性
能够检查參数,一直没注意这个语言特性,忽略了非常多细节,感谢 vitrox class Person( object ): def __init__( self, name ): if not isi ...
- 网络编程-Python高级语法-property属性
知识点: 一.什么是property属性? 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法,Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最 ...
- python中的property属性
目录 1. 什么是property属性 2. 简单的实例 3. property属性的有两种方式 3.1 装饰器方式 3.2 类属性方式,创建值为property对象的类属性 4. property属 ...
- python 中 property 属性的讲解及应用
Python中property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回 property属性的有两种方式: 1. 装饰器 即:在方法上应用装饰器 2. 类属性 即 ...
- python的property属性
最近看书中关于Python的property()内建函数属性内容时<python核心编程>解释的生僻难懂,但在网上看到了一篇关于property属性非常好的译文介绍. http://pyt ...
- 【转】python之property属性
1. 什么是property属性 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法 # ############### 定义 ############### class Foo: def ...
- python中property属性的介绍及其应用
Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回. 使用property修饰的实例方法被调用时,可以把它当做实例属性一样 property的 ...
随机推荐
- 20、uwp打包失败(All app package manifests in a bundle must declare the same values under the XPath *[local-name()='Package']/*[local-name()='Dependencies'])
在给 uwp工程打商店包的时候,遇到了一个异常: Error info: error 80080204: All app package manifests in a bundle must decl ...
- MOTIONEVENT的GETX()和GETRAWX()和VIEW的GETLEFT()3个方法的区别
- Centos7 ss搭建
1.安装pip Pip 是 Python 的包管理工具,下载ss十分方便,但是centos是没有pip的,我们需要安装一个. yum install python-setuptools & e ...
- 【转】MATLAB conv2函数的理解
另附:http://blog.csdn.net/anan1205/article/details/12313593 原文:http://blog.csdn.net/andrewseu/article/ ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- Windows + Ubuntu双系统时间不一致
在安装Ubuntu和Windows双系统的情况下,Ubuntu的时间总会和Windows的时间相差8小时,原因在于widows认为BIOS时间是本地时间,Ubuntu认为BIOS时间是UTC时间 su ...
- ASP.NET学习笔记(1)——VS自动引入命名空间快捷键
说明(2017-7-3 22:16:35) 1.在vs的“工具”->“选项”中,左侧树形菜单,“环境”下的“键盘”中设置快捷键. 在“显示命令包含”输入框内输入“显示智能标记”,找到“视图.显示 ...
- 【Unity笔记】制作小地图Minimap
真正的手把手教程,太棒了: http://forum.china.unity3d.com/thread-17192-1-1.html 或者是使用插件NJG MiniMap: http://www.ta ...
- C# if为false仍然进入方法体,==和qeual结果不一致
场景: 代码: if( e.Key == Key.Tab) { // ... } 断点调试:结果为false,进入方法体 ??? 更改为: if(Key.Tab.Equals(e.key)) { ...
- <[完整版]中国式价值投资>读书笔记
注重本金安全 股票价格的高级与股票的贵贱没有任何关系 同股同权的应该买便宜的 买未来有可能变得更大的优秀公司股票,只有他们的股价才有可能有持续向上的原动力 如果绝大多数投资者对股票高市盈率不是拒绝而是 ...