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的 ...
随机推荐
- 【C/C++】C/C++中Static的作用详述
在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. ❶先来介绍它的第一条也是最重要的一条:隐藏.当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可 ...
- Python实现二叉树的左中右序遍历
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/3/18 12:31 # @Author : baoshan # @Site ...
- 【Python】获取服务器时间
import http.client import time import os def get_webservertime(host): conn=http.client.HTTPConnectio ...
- idea 设置字体
1.设置 ui字体 修改编辑器的字体(也就是代码的字体):设置-Editor-Color&Font,默认的scheme是不可以更改的,你需要save as,建立一个新的(名字可以随意写个,My ...
- IP段对应表
IP段对应表 IP总数 子网掩码 C段个数 /30 4 255.255.255.252 1/64 /29 8 255.255.255.248 1/32 /28 16 255.255.255.240 ...
- C#学习笔记(17)——C#中接口的作用
说明(2017-7-17 23:50:48): 原文: 接口的作用 接口的作用(C#)(另一篇) C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体 ...
- php 裁剪图片类
<?php /* *说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形 * 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高 * writt ...
- 【转】Oracle 自定义函数语法与实例
原文地址:https://blog.csdn.net/libertine1993/article/details/47264211 Oracle自定义函数的语法如下: create or replac ...
- Redis Cluster集群搭建<原>
一.环境配置 一台window 7上安装虚拟机,虚拟机中安装的是centos系统. 二.目标 Redis集群搭建的方式有多种,根据集群逻辑的位置,大致可以分为三大类:基于客户端分片的Redis ...
- java 集合排序
Java API针对集合类型排序提供了两种支持:java.util.Collections.sort(java.util.List)java.util.Collections.sort(java.ut ...