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
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,
How the arguments are assembled depends on
a.xis transformed into the call:
A.xis transformed into the call:
ais an instance of super, then the binding
obj.__class__.__mro__for the base class
Band then invokes the descriptor with the call:
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.
See About this document... for information on suggesting changes.