Python descriptor 以及 内置property()函数
Python Descriptor
1, Python Descriptor是这样一个对象
它按照descriptor协议, 有这样的属性之一
def __get__(self, obj, type=None) # 会返回一个value
def __set__(self, obj, value) # 返回None
def __delete__(self, obj) # 返回None
这样的对象就是一个descriptor
2, descriptor的特性
假若有一个对象t, 我们去引用它的一个属性a
t.a
但是发现a是一个descriptor
那么不会返回a, 而是会去调用a相应的__get__, __set__, __delete__
那么什么情况调用那个呢?如下
- v = t.a <----> v = __get__(a, t)
- t.a = v <-----> __set__(a, t, v)
- del t.a <-----> __delete__(a, t)
3, descriptor是如何实现的
只有new-style objects或class的属性在被引用时,descriptor的特性才能起作用
从 object 派生的类就是 new-style class
class T(object):
pass
那么这大概是怎么回事呢?
是因为object有__getattribute__属性, 这个属性的实现确保了descriptor机制
所以如果我们重写了__getattribute__, 那么就可以消除descriptor机制
__getattribute__是如何实现的,以后探讨, 参考2中有一点点例子
内置函数 property()
Python有内置property()函数, 它可以直接做函数,也可以用来做装饰器, 它的使用方式如下, 例子来自参考3
class Test(object):
def getx(self):
return self._x
def setx(self, v):
self._x = v
def deletex(self):
del self._x x = property(getx, setx, deletex, ''' __doc__''')
而上面的代码等价于下面的
class Test(object):
@property
def x(self):
return self._x @x.setter
def x(self, v):
self._x = v @x.deleter
def x(self):
del self._x
对于Test的x属性,可以这么用
t = Test()
t.x = 5
print t.x
del t.x
那么为什么property()可以这么用,尤其是第二种中, x.setter和 x.deleter还可以做装饰器呢?
首先我们要先明白装饰器是什么
property()会返回一个Property对象, 然后我们来看一个用Python模拟的Property类的实现, 摘自参考1
class Property(object):
"Emulate PyProperty_Type() in Objects/descrobject.c" def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
if doc is None and fget is not None:
doc = fget.__doc__
self.__doc__ = doc def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError("unreadable attribute")
return self.fget(obj) def __set__(self, obj, value):
if self.fset is None:
raise AttributeError("can't set attribute")
self.fset(obj, value) def __delete__(self, obj):
if self.fdel is None:
raise AttributeError("can't delete attribute")
self.fdel(obj) def getter(self, fget):
return type(self)(fget, self.fset, self.fdel, self.__doc__) def setter(self, fset):
return type(self)(self.fget, fset, self.fdel, self.__doc__) def deleter(self, fdel):
return type(self)(self.fget, self.fset, fdel, self.__doc__)
认真看看就明白了
- Property对象是Descriptor
- Property.setter 和 Property.deleter 都是装饰器,他们和property一样,都是返回Property()对象,不同的是 @property设置 fget , setter和 deleter分别设置 fset, 和 fdel
-----------------------------------
很好的学习参考:
1, http://stackoverflow.com/questions/17330160/python-how-does-decorator-property-work
2, http://docs.python.org/3.2/howto/descriptor.html
3, http://docs.python.org/3.2/library/functions.html#property
Python descriptor 以及 内置property()函数的更多相关文章
- python 中的内置高级函数
1.map(function,iterable) map是把迭代对象依次进行函数运算,并返回. 例子: map返回的十分map对象,需要list()函数转化. 2.exec()函数 执行储存在字符串或 ...
- python 常见的内置函数
内置函数 接下来,我们就一起来看看python里的内置函数.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数.这 ...
- python之路——内置函数和匿名函数
阅读目录 楔子 内置函数 匿名函数 本章小结 楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们 ...
- python学习交流 - 内置函数使用方法和应用举例
内置函数 python提供了68个内置函数,在使用过程中用户不再需要定义函数来实现内置函数支持的功能.更重要的是内置函数的算法是经过python作者优化的,并且部分是使用c语言实现,通常来说使用内置函 ...
- python常用的内置函数哈哈
python常用的内置函数集合做一个归类用的时候可以查找 abs 返回数字x的绝对值或者x的摸 all (iterable)对于可迭代的对象iterable中所有元素x都有bool(x)为true,就 ...
- python常用的内置函数
python常用的内置函数集合做一个归类用的时候可以查找- abs 返回数字x的绝对值或者x的摸 - all (iterable)对于可迭代的对象iterable中所有元素x都有bool(x)为tru ...
- 十六. Python基础(16)--内置函数-2
十六. Python基础(16)--内置函数-2 1 ● 内置函数format() Convert a value to a "formatted" representation. ...
- 十五. Python基础(15)--内置函数-1
十五. Python基础(15)--内置函数-1 1 ● eval(), exec(), compile() 执行字符串数据类型的python代码 检测#import os 'import' in c ...
- Python的常用内置函数介绍
Python的常用内置函数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.取绝对值(abs) #!/usr/bin/env python #_*_coding:utf-8_ ...
随机推荐
- 畅通工程 HDU - 1863 最小生成树模板
两个模板: kruskal #include<stdio.h> #include<queue> #include<algorithm> #include<io ...
- MySQL在linux上的rpm包方式安装方法
1.下载上传mysql server和client rpm包: [root@faspdev mnt]# ls MySQL-client-5.5.53-1.el6.x86_64.rpm MySQL-se ...
- python数据结构之哈希表
哈希表(Hash table) 众所周知,HashMap是一个用于存储Key-Value键值对的集合,每一个键值对也叫做Entry.这些个键值对(Entry)分散存储在一个数组当中,这个数组就是Has ...
- matplotlib基本使用方法
[微语]人生有可为之事,也有不可为之事.可为之事,当尽力为之,此谓尽性,不可为之事,当尽心为之,此谓知命. 三人行必有我师 官方参考API:https://matplotlib.org/tutoria ...
- python center() 函数
center Python center() 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串.默认填充字符为空格. 语法 center()方法语法: str.center(widt ...
- 下载google play上最新版的apk
注意,是下载最新版的方法,不是像很多网站下的是旧版本 http://techapple.net/2014/09/3-websites-directly-download-apk-google-play ...
- ubuntu系统下怎么安装gcc编译器
你安装一个名字叫做build-essential的软件包,就可以一次将编译器.make工具.所有的编程头文件.函数库等东东全部安装上,其中也包括gcc编译器,这是非常稳妥的安装方式,安装命令是用roo ...
- numpy.random.rand()/randn()/randint()/normal()/choice()/RandomState()
这玩意用了很多次,但每次用还是容易混淆,今天来总结mark一下~~~ 1. numpy.random.rand(d0,d1,...,dn) 生成一个[0,1)之间的随机数或N维数组 np.random ...
- localhost和本机IP和127.0.0.1之间的区别
参考出处:https://www.zhihu.com/question/23940717 localhost 是个域名,不是地址,它可以被配置为任意的 IP 地址,不过通常情况下都指向 127.0.0 ...
- smali注入常用代码
注入代码需要注意寄存器个数.1.插入log信息 const-string v2,"SN" invoke-static {v2,v0}, Landroid/util/Log;-> ...