python descriptor override
来源:互联网 发布:淘宝客类目佣金 编辑:程序博客网 时间:2024/05/29 16:05
.4.2.3 Invoking Descriptors
In general, a descriptor is an object attribute with ``binding behavior'', one whose attribute access has been overridden by methods in the descriptor protocol: __get__(), __set__(), and __delete__(). If any of those methods are defined for an object, it is said to be a descriptor.
The default behavior for attribute access is to get, set, or delete the attribute from an object's dictionary. For instance, a.x
has a lookup chain starting with a.__dict__['x']
, then type(a).__dict__['x']
, and continuing through the base classes of type(a)
excluding metaclasses.
However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. Note that descriptors are only invoked for new style objects or classes (ones that subclass object() or type()).
The starting point for descriptor invocation is a binding, a.x
. How the arguments are assembled depends on a
:
- Direct Call
- The simplest and least common call is when user code directly invokes a descriptor method:
x.__get__(a)
. - Instance Binding
- If binding to a new-style object instance,
a.x
is transformed into the call:type(a).__dict__['x'].__get__(a, type(a))
. - Class Binding
- If binding to a new-style class,
A.x
is transformed into the call:A.__dict__['x'].__get__(None, A)
. - Super Binding
- If
a
is an instance of super, then the bindingsuper(B, obj).m()
searchesobj.__class__.__mro__
for the base classA
immediately precedingB
and then invokes the descriptor with the call:A.__dict__['m'].__get__(obj, A)
.
For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. Data descriptors define both __get__() and __set__(). Non-data descriptors have just the __get__()method. Data descriptors always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances.
Python methods (including staticmethod() and classmethod()) are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class.
The property() function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property.
- python descriptor override
- Python descriptor
- python descriptor
- Python descriptor
- python descriptor
- python descriptor
- Python descriptor
- Python descriptor
- Python的descriptor
- python的descriptor
- Python的descriptor
- Python的descriptor
- Python的descriptor
- python中的descriptor
- python描述符Descriptor
- Python 描述器 descriptor
- python 描述符descriptor
- python描述符 descriptor
- SQL找出某个字符第几个出现的位置
- mongo 常用的命令
- Shell 命令--paste--总结自《Linux Shell 脚本攻略》
- eclipse的使用、优化配置
- IOS中系统唯一标识ID
- python descriptor override
- HDU 1116 Play on Words (歐拉迴路 + 并查集)
- 【JS】【Core】
- 使用JS中的Date实现不同的时间格式
- html5+javascript視頻點碎裂效果
- linux IP 命令使用举例
- bzoj2243: [SDOI2011]染色 树链剖分
- 如何配置能让fiddler抓去https的请求?
- C语言中数组