python Descriptor (描述符)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Descriptor (描述符)相关的知识,希望对你有一定的参考价值。
简介:
python 描述符是新式类(继承自object)中的语言协议,基于描述符可以提供更佳优雅的解决方案。
python的classmethod, staticmethod, property都是基于描述符建立的。
描述符的协议:
定义了__set__, __get__, __delete__3个方法中任何一个方法的object可以作为描述符.
描述符分类:
同时定义了__set__,__get__被叫做data descriptor.
只定义了__get__被叫做no-data descriptor.
2种描述符的区别:
Data and non-data descriptors differ in how overrides are calculated with respect to entries in an instance’s dictionary. If an instance’s dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance’s dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence.
在 attrubuite lookup过程中 :
如果对象属性有与data-descriptor同名的属性,data-descriptor优先于对象属性.
如果对象属性有与no-data descriptor同名的属性,对象属性优先。
触发描述符的调用:
A descriptor can be called directly by its method name. For example, d.__get__(obj)
.
Alternatively, it is more common for a descriptor to be invoked automatically upon attribute access. For example, obj.d
looks up d
in the dictionary of obj
. If d
defines the method __get__()
, thend.__get__(obj)
is invoked according to the precedence rules listed below.
The details of invocation depend on whether obj
is an object or a class. Either way, descriptors only work for new style objects and classes. A class is new style if it is a subclass of object
.
For objects, the machinery is in object.__getattribute__()
which transforms b.x
into type(b).__dict__[‘x‘].__get__(b, type(b))
. The implementation works through a precedence chain that gives data descriptors priority over instance variables, instance variables priority over non-data descriptors, and assigns lowest priority to __getattr__()
if provided
The important points to remember are:
- descriptors are invoked by the
__getattribute__()
method - overriding
__getattribute__()
prevents automatic descriptor calls __getattribute__()
is only available with new style classes and objectsobject.__getattribute__()
andtype.__getattribute__()
make different calls to__get__()
.- data descriptors always override instance dictionaries.
- non-data descriptors may be overridden by instance dictionaries.
以上是关于python Descriptor (描述符)的主要内容,如果未能解决你的问题,请参考以下文章