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的 ...
随机推荐
- Lintcode: First Bad Version 解题报告
First Bad Version http://lintcode.com/en/problem/first-bad-version The code base version is an integ ...
- 【Java】PreparedStatement VS Statement
创建时: Statement statement = conn.createStatement(); PreparedStatement preStatement = conn.prepareS ...
- Linux下字符集的安装
目前环境中经常会遇到编码转化的问题,UTF-8跟GB2312也有问题.只得在Linux上安装GB2312(在Linux操作系统上又称zh_CN.GB2312)的字符集,具体请看下文. Linux下几个 ...
- 将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药
将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药 @echo off echo 将该目录下所有.cs文件的内容合并到一个 code.cs 文件中! pau ...
- jsp传给java属性,java生成json串,方便以后取出来
前台代码 $.ajax({ url : '<%=basePath%>userorderother/canUpdateCust.do', type : 'POST', data: {'kdc ...
- 【Unity Shader】七、透明的Transparent Shader
学习资料: http://www.sikiedu.com/course/37/task/459/show# 本例的代码基于上一篇文章,添加透明效果.为了便于区分新增的部分,该部分使用和红色加粗字体. ...
- C++学习笔记(HelloWorld,类型和值)
现在有一个从控制台读取输入的小程序: #include "../std_lib_facilities.h" int main() { cout << "Ple ...
- 回顾一下Unix哲学
Unix哲学是一些先哲们多方位阐述的,有多种说法.可以概括为以下几点: 模块原则:使用简洁的接口拼合简单的部件. 清晰原则:清晰胜于机巧. 组合原则:设计时考虑拼接组合. 分离原则:策略同机制分离,接 ...
- Tomcat配置JMX远程监控(Windown7 Linxu)
一:Window7下配置方式. 1.配置catalina.bat 在第一行加入下面配置 注意下面这些配置要在一行,注意包含空格. set JAVA_OPTS=-Dcom.sun.management. ...
- Tomcat性能优化(二) ExpiresFilter设置浏览器缓存
Tomcat性能调优 通过ExpiresFilter设置资源缓存 [官方文档] http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#E ...