python中基于descriptor的一些概念(下)
@python中基于descriptor的一些概念(下)
3. Descriptor介绍
3.1 Descriptor代码示例
"""创建一个Descriptor类,用来打印出访问它的操作信息
"""
def __init__(self, initval=None, name='var'):
self.val = initval
self.name = name
def __get__(self, obj, objtype):
print 'Retrieving', self.name
return self.val
def __set__(self, obj, val):
print 'Updating' , self.name
self.val = val
#使用Descriptor
class MyClass(object):
#生成一个Descriptor实例,赋值给类MyClass的x属性
x = RevealAccess(10, 'var "x"')
y = 5 #普通类属性

3.2 定义
3.3 Descriptor Protocol(协议)
3.4 Descriptor调用方法
"模拟type.__getattribute__()实现"
#通过默认方式查找到目标
v = object.__getattribute__(self, key)
#如果目标属性含有__get__方法,则表示它是一个descriptor
if hasattr(v, '__get__'):
#优先使用descriptor中定义的方法返回值
return v.__get__(None, self)
#如果不是descriptor,就按默认的方式返回
return v
- descriptor是被__getattribute__方法调用的。
- 重写__getattribute__方法,会阻止自动的descriptor调用,必要时需要你自己加上去。
- __getattribute__方法只在新式类和新式实例中有用。
- object.__getattribute__和class.__getattribute__会用不一样的方式调用__get__
- data descriptors总是覆盖instance dictionary
- non-data descriptors有可能被instance dictionary覆盖
4. 基于Descriptor实现的功能
- Property
- 绑定和非绑定方法
- 静态方法
- 类方法
- super
4.1 property
def getX(self):
print 'get x'
return self.__x
def setX(self, value):
print 'set x', value
self.__x = value
def delX(self):
print 'del x'
del self.__x
x = property(getX, setX, delX, "This is 'x' property.")
def __init__(self, width, heigth):
self.width = width
self.heigth = heigth
def getArea(self):
return self.width * self.heigth
area = property(getArea, doc='area of the rectangle')
"模拟在Objects/descrobject.c文件中的PyProperty_Type()函数"
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
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)
4.2 函数和方法,绑定与非绑定
. . .
def __get__(self, obj, objtype=None):
"模拟Objects/funcobject.c文件中的func_descr_get()"
return types.MethodType(self, obj, objtype)

4.3 super

def __init__(self, type, obj=None):
self.__type__ = type
self.__obj__ = obj
def __get__(self, obj, type=None):
if self.__obj__ is None and obj is not None:
return Super(self.__type__, obj)
else:
return self
def __getattr__(self, attr):
if isinstance(self.__obj__, self.__type__):
starttype = self.__obj__.__class__
else:
starttype = self.__obj__
mro = iter(starttype.__mro__)
for cls in mro:
if cls is self.__type__:
break
# Note: mro is an iterator, so the second loop
# picks up where the first one left off!
for cls in mro:
if attr in cls.__dict__:
x = cls.__dict__[attr]
if hasattr(x, "__get__"):
x = x.__get__(self.__obj__)
return x
raise AttributeError, attr
5. 结尾
python中基于descriptor的一些概念(下)的更多相关文章
- python中基于descriptor的一些概念
python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2.2.1 静态方法 2.2.2 类方法 2.3 新式类(n ...
- python中基于descriptor的一些概念(上)
@python中基于descriptor的一些概念(上) python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2 ...
- python 中关于descriptor的一些知识问题
这个问题从早上日常扫segmentfault上问题开始 有个问题是 class C(object): @classmethod def m(): pass m()是类方法,调用代码如下: C.m() ...
- python中的 descriptor
学好和用好python, descriptor是必须跨越过去的一个点,现在虽然Python书籍花样百出,但是似乎都是在介绍一些Python库而已,对Python语言本身的关注很少,或者即使关注了,但是 ...
- 轻松理解python中的闭包和装饰器 (下)
在 上篇 我们讲了python将函数做为返回值和闭包的概念,下面我们继续讲解函数做参数和装饰器,这个功能相当方便实用,可以极大地简化代码,就让我们go on吧! 能接受函数做参数的函数我们称之为高阶函 ...
- python中模块与包的概念
在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多 ...
- python中模块和包的概念
1.模块 一个.py文件就是一个模块.这个文件的名字是:模块名.py.由此可见在python中,文件名和模块名的差别只是有没有后缀.有后缀是文件名,没有后缀是模块名. 每个文件(每个模块)都是一个独立 ...
- (数据科学学习手札136)Python中基于joblib实现极简并行计算加速
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 我们在日常使用Python进行各种数据计算 ...
- python中基于tcp协议的通信(数据传输)
tcp协议:流式协议(以数据流的形式通信传输).安全协议(收发信息都需收到确认信息才能完成收发,是一种双向通道的通信) tcp协议在OSI七层协议中属于传输层,它上承用户层的数据收发,下启网络层.数据 ...
随机推荐
- java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/20 from pid=711, uid=10074 requires android.permission.READ_
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider ur ...
- 一个JNI的helloworld小demo
最近想学习一下jni,在网上看了一些demo,自己也操作了一遍,首先我将我自己学习的demo网站贴出来:https://blog.csdn.net/lwcloud/article/details/78 ...
- 撩课-Web大前端每天5道面试题-Day3
1. javascript的typeof返回哪些数据类型? 答案: undefined string boolean number symbol(ES6) Object Function 2. 列举3 ...
- java核心技术-NIO
1.reactor(反应器)模式 使用单线程模拟多线程,提高资源利用率和程序的效率,增加系统吞吐量.下面例子比较形象的说明了什么是反应器模式: 一个老板经营一个饭店, 传统模式 - 来一个客人安排一个 ...
- Groovy中的操作符重载
操作者 方法 a + b a.plus(b)中 a - b a.minus(b)中 a * b a.multiply(b)中 a ** b a.power(b)中 a / b a.div(b)中 a ...
- csu 1356 Catch bfs(vector)
1356: Catch Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 96 Solved: 40[Submit][Status][Web Board] ...
- Fill Table Row(it’s an IQ test question)
Here is a table include the 2 rows. And the cells in the first row have been filled with 0~4. Now yo ...
- hashlib 文件校验,MD5动态加盐返回加密后字符
hashlib 文件校验 # for循环校验 import hashlib def check_md5(file): ret = hashlib.md5() with open(file, mode= ...
- [COCI2006-2007#1] Bond
状压DP \(dp[i]\)表示当前选人状态为\(i\)且选择了前\(i.count()\)个物品时最大的概率 #include"cstdio" #include"cst ...
- AIX 6.1记录
安装Oracle需要开启远程桌面进行访问 1. X Windows需要如下软件包才能正常运行 lslpp -l X11.apps.rte X11.apps.xterm X11.base.rte X11 ...