descriptor简介

  在python中,如果一个新式类定义了__get__, __set__, __delete__方法中的一个或者多个,那么称之为descriptor。descriptor有分为data descriptor与non-data descriptor, descriptor通常用来改变默认的属性访问(attribute lookup),这部分会在下一遍文章中介绍。注意 ,descriptor的实例是一定是的属性(class attribute)

  这三个特殊的函数签名是这样的:

  object.__get__(selfinstanceowner):return value
  object.__set__(selfinstancevalue):return None
  object.__delete__(selfinstance): return None

  

  下面的代码展示了简单的用法:
 
  1. # -*- coding: utf-8 -*-
  2. class Des(object):
  3. def __init__(self, init_value):
  4. self.value = init_value
  5.  
  6. def __get__(self, instance, typ):
  7. print('call __get__', instance, typ)
  8. return self.value
  9.  
  10. def __set__(self, instance, value):
  11. print ('call __set__', instance, value)
  12. self.value = value
  13.  
  14. def __delete__(self, instance):
  15. print ('call __delete__', instance)
  16.  
  17. class Widget(object):
  18. t = Des(1)
  19.  
  20. def main():
  21. w = Widget()
  22. print type(w.t)
  23. w.t = 1
  24. print w.t, Widget.t
  25. del w.t
  26.  
  27. if __name__=='__main__':
  28. main()

  运行结果如下:

('call __get__', <__main__.Widget object at 0x02868570>, <class '__main__.Widget'>)
<type 'int'>

('call __set__', <__main__.Widget object at 0x02868570>, 1)

('call __get__', <__main__.Widget object at 0x02868570>, <class '__main__.Widget'>)
1 ('call __get__', None, <class '__main__.Widget'>)

1

('call __delete__', <__main__.Widget object at 0x02868570>)

  从输出结果可以看到,对于这个三个特殊函数,形参instance是descriptor实例所在的类的实例(w), 而形参owner就是这个类(widget)

  w.t 等价于 Pro.__get__(t, w, Widget).而Widget.t 等价于 Pro.__get__(t, None, Widget)

descriptor注意事项

  需要注意的是, descriptor的实例一定是类的属性,因此使用的时候需要自行区分实例。比如下面这个例子,我们需要保证以下属性不超过一定的阈值。

  1. class MaxValDes(object):
  2. def __init__(self, inti_val, max_val):
  3. self.value = inti_val
  4. self.max_val = max_val
  5.  
  6. def __get__(self, instance, typ):
  7. return self.value
  8.  
  9. def __set__(self, instance, value):
  10. self.value= min(self.max_val, value)
  11.  
  12. class Widget(object):
  13. a = MaxValDes(0, 10)
  14.  
  15. if __name__ == '__main__':
  16. w0 = Widget()
  17. print 'inited w0', w0.a
  18. w0.a = 123
  19. print 'after set w0',w0.a
  20. w1 = Widget()
  21. print 'inited w1', w1.a

  代码很简单,我们通过MaxValDes这个descriptor来保证属性的值不超过一定的范围。运行结果如下:

inited w0 0
after set w0 10
inited w1 10

  可以看到,对w0.a的赋值符合预期,但是w1.a的值却不是0,而是同w0.a一样。这就是因为,a是类Widget的类属性, Widget的实例并没有'a'这个属性,可以通过__dict__查看。

  那么要怎么修改才符合预期呢,看下面的代码:

  1. class MaxValDes(object):
  2. def __init__(self, attr, max_val):
  3. self.attr = attr
  4. self.max_val = max_val
  5.  
  6. def __get__(self, instance, typ):
  7. return instance.__dict__[self.attr]
  8.  
  9. def __set__(self, instance, value):
  10. instance.__dict__[self.attr] = min(self.max_val, value)
  11.  
  12. class Widget(object):
  13. a = MaxValDes('a', 10)
  14. b = MaxValDes('b', 12)
  15. def __init__(self):
  16. self.a = 0
  17. self.b = 1
  18.  
  19. if __name__ == '__main__':
  20. w0 = Widget()
  21. print 'inited w0', w0.a, w0.b
  22. w0.a = 123
  23. w0.b = 123
  24. print 'after set w0',w0.a, w0.b
  25.  
  26. w1 = Widget()
  27. print 'inited w1', w1.a, w1.b

  运行结果如下:

inited w0 0 1
after set w0 10 12
inited w0 0 1

  可以看到,运行结果比较符合预期,w0、w1两个实例互不干扰。上面的代码中有两点需要注意:

  第一:第7、10行都是通过instance.__dict__来取值、赋值,而不是调用getattr、setattr,否则会递归调用,死循环。

  第二:现在类和类的实例都拥有‘a’属性,不过w0.a调用的是类属性‘a',具体原因参见下一篇文章

descriptor应用场景

  其实从上面的例子可以看出,descriptor主要用于控制属性的访问(读、写、删除)。python doc里面有写到,property()就是一个data descriptor实现(可参见这个文档)。 python2.2中,大量新式类的实现都基于descriptor  

They are the mechanism behind properties, methods, static methods, class methods, and super(). They are used throughout Python itself to implement the new style classes introduced in version 2.2.

 
  在实践中,我们有可能需要监控或者限制对属性的访问。比如,对象的一个属性被“莫名其妙”地修改了,但搜索所有文件有找不到可以的地方,那么我们可以通过__setattr__(self, k, v)方法,对于我们关心的 k 打印出调用栈。另外,也可以用property,示例代码如下:
  1. class TestProperty(object):
  2. def __init__(self):
  3. self.__a = 1
  4.  
  5. @property
  6. def a(self):
  7. return self.__a
  8.  
  9. @a.setter
  10. def a(self, v):
  11. print('output call stack here')
  12. self.__a = v
  13.  
  14. if __name__=='__main__':
  15. t = TestProperty()
  16. print t.a
  17. t.a = 2
  18. print t.a

  如果需要禁止对属性赋值,或者对新的值做检查,也很容易修改上面的代码实现

  既然有了property,那什么时候还需要descriptor呢?property最大的问题在于不能重复使用,即对每个属性都需要property装饰,代码重复冗余。而使用descriptor,把相同的逻辑封装到一个单独的类,使用起来方便多了。详细的示例可以参见这篇文章

  笔者之前看bottle.py源码的时候,看到这么一个descriptor使用,部分源代码和测试代码如下:
 
  1. import functools, time
  2. class cached_property(object):
  3. """ A property that is only computed once per instance and then replaces
  4. itself with an ordinary attribute. Deleting the attribute resets the
  5. property. """
  6.  
  7. def __init__(self, func):
  8. functools.update_wrapper(self, func)
  9. self.func = func
  10.  
  11. def __get__(self, obj, cls):
  12. if obj is None: return self
  13. value = obj.__dict__[self.func.__name__] = self.func(obj)
  14. return value
  15.  
  16. class TestClz(object):
  17. @cached_property
  18. def complex_calc(self):
  19. print 'very complex_calc'
  20. return sum(range(100))
  21.  
  22. if __name__=='__main__':
  23. t = TestClz()
  24. print '>>> first call'
  25. print t.complex_calc
  26. print '>>> second call'
  27. print t.complex_calc
  运行结果如下:
     >>> first call
    very complex_calc
    4950
    >>> second call
    4950
 
  注意两点:

  第一,在访问complex_calc的时候并没有使用函数调用(没有括号);

  第二,第一次调用的时候打印了“very complex_calc”,第二次没有。
 
  笔者也是因为这段代码开始学习descriptor,但看懂这段代码还需要了解Python的属性查找顺序,下一篇文章会对此简单介绍。
 

references

(0)Implementing Descriptors, python2.7 doc
(2)Python描述符(descriptor)解密, http://www.geekfan.net/7862/

python描述符 descriptor的更多相关文章

  1. Python 描述符(descriptor) 杂记

    转自:https://blog.tonyseek.com/post/notes-about-python-descriptor/ Python 引入的“描述符”(descriptor)语法特性真的很黄 ...

  2. python描述符descriptor(一)

    Python 描述符是一种创建托管属性的方法.每当一个属性被查询时,一个动作就会发生.这个动作默认是get,set或者delete.不过,有时候某个应用可能会有 更多的需求,需要你设计一些更复杂的动作 ...

  3. python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解

     1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...

  4. Python描述符 (descriptor) 详解

    1.什么是描述符? python描述符是一个“绑定行为”的对象属性,在描述符协议中,它可以通过方法重写属性的访问.这些方法有 __get__(), __set__(), 和__delete__().如 ...

  5. Python 描述符(Descriptor) 附实例

    在 Python 众多原生特性中,描述符可能是最少被自定义的特性之一,但它在底层实现的方法和属性却无时不刻被使用着,它优雅的实现方式体现出 Python 简洁之美. 定义 一个描述符是一个有" ...

  6. Python 描述符 (descriptor)

    1.什么是描述符? 描述符是Python新式类的关键点之一,它为对象属性提供强大的API,你可以认为描述符是表示对象属性的一个代理.当需要属性时,可根据你遇到的情况,通过描述符进行访问他(摘自Pyth ...

  7. python描述符descriptor(二)

    python内置的描述符 python有些内置的描述符对象,property.staticmethod.classmethod,python实现如下: class Property(object): ...

  8. 【python】描述符descriptor

    开始看官方文档,各种看不懂,只看到一句Properties, bound and unbound methods, static methods, and class methods are all ...

  9. 杂项之python描述符协议

    杂项之python描述符协议 本节内容 由来 描述符协议概念 类的静态方法及类方法实现原理 类作为装饰器使用 1. 由来 闲来无事去看了看django中的内置分页方法,发现里面用到了类作为装饰器来使用 ...

随机推荐

  1. IEqualityComparer<T>接口

    IEqualityComparer<T>接口的对象的主要作用在于自定义判断两个对象是否相等. 其中最常用的方法: bool Equals(T x, T y); 实现该方法用于比较两个对象是 ...

  2. Decimal

    Description 任意一个分数都是有理数,对于任意一个有限小数,我们都可以表示成一个无限循环小数的形式(在其末尾添加0),对于任意一个无限循环小数都可以转化成一个分数.现在你的任务就是将任意一个 ...

  3. 深入super,看Python如何解决钻石继承难题

    1.   Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通方法和super方法 假设Base是基类 class Base(object): def __init_ ...

  4. Ubuntu 无线连接能上网,但是有线连接不能上

    这两天装Ubuntu,遇到小问题.最头疼的还是上网,过去我装了Ubuntu时,都是插上网线就能直接上网,这次就不行了. 我刚点开一个网页,接下来点就不能上了,但是无线连接就可以正常上网. 我在一个论坛 ...

  5. JAVA中运用数组的四种排序方法

    JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法. 快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现. 冒泡法是运用遍历数组进 ...

  6. stardict词典(星际译王)

    sudo apt-get install stardict 下载词库: http://abloz.com/huzheng/stardict-dic/zh_CN/ 把下载的压缩包解压,以a为例cd /u ...

  7. 利用java反射机制 读取配置文件 实现动态类载入以及动态类型转换

    作者:54dabang 在spring的学习过程之中,我们能够看出通过配置文件来动态管理bean对象的优点(松耦合 能够让零散部分组成一个总体,而这些总体并不在意之间彼此的细节,从而达到了真正的物理上 ...

  8. oracle 同样数据删除(仅仅留一条)

    DELETE FROM reg_user t1 WHERE user_name='9527008' and rowid > ( SELECT min(rowid) FROM location t ...

  9. Codeforces Round #260 (Div. 2)A. Laptops

    A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  10. Android 关于倒计时功能的实现

    关于倒计时的实现,可以说有很多的方法,比较常见的就是Timer+TimerTask+Handler了,或者还可以配合Runnable.例如下面的代码: import java.util.Timer; ...