Descriptor Howto Guide: Guido Van Rossum and The Python Development Team
Descriptor Howto Guide: Guido Van Rossum and The Python Development Team
Descriptor Howto Guide: Guido Van Rossum and The Python Development Team
Release 2.7.15
Contents
1 Abstract 2
3 Descriptor Protocol 2
4 Invoking Descriptors 3
5 Descriptor Example 4
6 Properties 4
Contents
1 Abstract
Defines descriptors, summarizes the protocol, and shows how descriptors are called. Examines a custom
descriptor and several built-in python descriptors including functions, properties, static methods, and class
methods. Shows how each works by giving a pure Python equivalent and a sample application.
Learning about descriptors not only provides access to a larger toolset, it creates a deeper understanding of
how Python works and an appreciation for the elegance of its design.
3 Descriptor Protocol
descr.__get__(self, obj, type=None) --> value
descr.__set__(self, obj, value) --> None
descr.__delete__(self, obj) --> None
That is all there is to it. Define any of these methods and an object is considered a descriptor and can
override default behavior upon being looked up as an attribute.
If an object defines both __get__() and __set__(), it is considered a data descriptor. Descriptors that
only define __get__() are called non-data descriptors (they are typically used for methods but other uses
are possible).
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.
To make a read-only data descriptor, define both __get__() and __set__() with the __set__() raising
an AttributeError when called. Defining the __set__() method with an exception raising placeholder is
enough to make it a data descriptor.
4 Invoking Descriptors
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__(), then d.__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 full C implementation can be found in
PyObject_GenericGetAttr() in Objects/object.c.
For classes, the machinery is in type.__getattribute__() which transforms B.x into B.__dict__['x'].
__get__(None, B). In pure Python, it looks like:
The protocol is simple and offers exciting possibilities. Several use cases are so common that they have been
packaged into individual function calls. Properties, bound and unbound methods, static methods, and class
methods are all based on the descriptor protocol.
6 Properties
Calling property() is a succinct way of building a data descriptor that triggers function calls upon access
to an attribute. Its signature is:
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
To see how property() is implemented in terms of the descriptor protocol, here is a pure Python equivalent:
class Property(object):
"Emulate PyProperty_Type() in Objects/descrobject.c"
The property() builtin helps whenever a user interface has granted attribute access and then subsequent
changes require the intervention of a method.
For instance, a spreadsheet class may grant access to a cell value through Cell('b10').value. Subsequent
improvements to the program require the cell to be recalculated on every access; however, the programmer
does not want to affect existing client code accessing the attribute directly. The solution is to wrap access
to the value attribute in a property data descriptor:
class Cell(object):
. . .
def getvalue(self):
"Recalculate the cell before returning value"
self.recalc()
return self._value
value = property(getvalue)
7 Functions and Methods
Python’s object oriented features are built upon a function based environment. Using non-data descriptors,
the two are merged seamlessly.
Class dictionaries store methods as functions. In a class definition, methods are written using def and
lambda, the usual tools for creating functions. The only difference from regular functions is that the first
argument is reserved for the object instance. By Python convention, the instance reference is called self but
may be called this or any other variable name.
To support method calls, functions include the __get__() method for binding methods during attribute
access. This means that all functions are non-data descriptors which return bound or unbound methods
depending whether they are invoked from an object or a class. In pure python, it works like this:
class Function(object):
. . .
def __get__(self, obj, objtype=None):
"Simulate func_descr_get() in Objects/funcobject.c"
return types.MethodType(self, obj, objtype)
Running the interpreter shows how the function descriptor works in practice:
The output suggests that bound and unbound methods are two different types. While they could have been
implemented that way, the actual C implementation of PyMethod_Type in Objects/classobject.c is a single
object with two different representations depending on whether the im_self field is set or is NULL (the C
equivalent of None).
Likewise, the effects of calling a method object depend on the im_self field. If set (meaning bound), the
original function (stored in the im_func field) is called as expected with the first argument set to the instance.
If unbound, all of the arguments are passed unchanged to the original function. The actual C implementation
of instancemethod_call() is only slightly more complex in that it includes some type checking.
Static methods return the underlying function without changes. Calling either c.f or C.f is the equivalent
of a direct lookup into object.__getattribute__(c, "f") or object.__getattribute__(C, "f"). As a
result, the function becomes identically accessible from either an object or a class.
Good candidates for static methods are methods that do not reference the self variable.
For instance, a statistics package may include a container class for experimental data. The class provides
normal methods for computing the average, mean, median, and other descriptive statistics that depend on
the data. However, there may be useful functions which are conceptually related but do not depend on
the data. For instance, erf(x) is handy conversion routine that comes up in statistical work but does not
directly depend on a particular dataset. It can be called either from an object or the class: s.erf(1.5) -->
.9332 or Sample.erf(1.5) --> .9332.
Since staticmethods return the underlying function with no changes, the example calls are unexciting:
Using the non-data descriptor protocol, a pure Python version of staticmethod() would look like this:
class StaticMethod(object):
"Emulate PyStaticMethod_Type() in Objects/funcobject.c"
Unlike static methods, class methods prepend the class reference to the argument list before calling the
function. This format is the same for whether the caller is an object or a class:
This behavior is useful whenever the function only needs to have a class reference and does not care about
any underlying data. One use for classmethods is to create alternate class constructors. In Python 2.3, the
classmethod dict.fromkeys() creates a new dictionary from a list of keys. The pure Python equivalent is:
class Dict(object):
. . .
def fromkeys(klass, iterable, value=None):
"Emulate dict_fromkeys() in Objects/dictobject.c"
d = klass()
for key in iterable:
d[key] = value
return d
fromkeys = classmethod(fromkeys)
>>> Dict.fromkeys('abracadabra')
{'a': None, 'r': None, 'b': None, 'c': None, 'd': None}
Using the non-data descriptor protocol, a pure Python version of classmethod() would look like this:
class ClassMethod(object):
"Emulate PyClassMethod_Type() in Objects/funcobject.c"