Descriptor HowTo Guide¶
- Author
Raymond Hettinger
- Contact
<python at rcn dot com>
Contents
Abstract¶
Defines descriptors, summarizes the protocol, and shows how descriptors arecalled. Examines a custom descriptor and several built-in Python descriptorsincluding functions, properties, static methods, and class methods. Shows howeach works by giving a pure Python equivalent and a sample application.
Learning about descriptors not only provides access to a larger toolset, itcreates a deeper understanding of how Python works and an appreciation for theelegance of its design.
Definition and Introduction¶
In general, a descriptor is an object attribute with “binding behavior”, onewhose attribute access has been overridden by methods in the descriptorprotocol. Those methods are__get__(),__set__(), and__delete__(). If any of those methods are defined for an object, it issaid to be a descriptor.
The default behavior for attribute access is to get, set, or delete theattribute from an object’s dictionary. For instance,a.x has a lookup chainstarting witha.__dict__['x'], thentype(a).__dict__['x'], andcontinuing through the base classes oftype(a) excluding metaclasses. If thelooked-up value is an object defining one of the descriptor methods, then Pythonmay override the default behavior and invoke the descriptor method instead.Where this occurs in the precedence chain depends on which descriptor methodswere defined.
Descriptors are a powerful, general purpose protocol. They are the mechanismbehind properties, methods, static methods, class methods, andsuper().They are used throughout Python itself to implement the new style classesintroduced in version 2.2. Descriptors simplify the underlying C-code and offera flexible set of new tools for everyday Python programs.
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 isconsidered a descriptor and can override default behavior upon being looked upas an attribute.
If an object defines both__get__() and__set__(), it is considereda data descriptor. Descriptors that only define__get__() are callednon-data descriptors (they are typically used for methods but other uses arepossible).
Data and non-data descriptors differ in how overrides are calculated withrespect to entries in an instance’s dictionary. If an instance’s dictionaryhas an entry with the same name as a data descriptor, the data descriptortakes precedence. If an instance’s dictionary has an entry with the samename 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 anAttributeError whencalled. Defining the__set__() method with an exception raisingplaceholder is enough to make it a data descriptor.
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 automaticallyupon attribute access. For example,obj.d looks upd in the dictionaryofobj. Ifd defines the method__get__(), thend.__get__(obj)is invoked according to the precedence rules listed below.
The details of invocation depend on whetherobj is an object or a class.
For objects, the machinery is inobject.__getattribute__() whichtransformsb.x intotype(b).__dict__['x'].__get__(b,type(b)). Theimplementation works through a precedence chain that gives data descriptorspriority over instance variables, instance variables priority over non-datadescriptors, and assigns lowest priority to__getattr__() if provided.The full C implementation can be found inPyObject_GenericGetAttr() inObjects/object.c.
For classes, the machinery is intype.__getattribute__() which transformsB.x intoB.__dict__['x'].__get__(None,B). In pure Python, it lookslike:
def__getattribute__(self,key):"Emulate type_getattro() in Objects/typeobject.c"v=object.__getattribute__(self,key)ifhasattr(v,'__get__'):returnv.__get__(None,self)returnv
The important points to remember are:
descriptors are invoked by the
__getattribute__()methodoverriding
__getattribute__()prevents automatic descriptor callsobject.__getattribute__()andtype.__getattribute__()makedifferent calls to__get__().data descriptors always override instance dictionaries.
non-data descriptors may be overridden by instance dictionaries.
The object returned bysuper() also has a custom__getattribute__()method for invoking descriptors. The callsuper(B,obj).m() searchesobj.__class__.__mro__ for the base classA immediately followingBand then returnsA.__dict__['m'].__get__(obj,B). If not a descriptor,m is returned unchanged. If not in the dictionary,m reverts to asearch usingobject.__getattribute__().
The implementation details are insuper_getattro() inObjects/typeobject.c. and a pure Python equivalent can be found inGuido’s Tutorial.
The details above show that the mechanism for descriptors is embedded in the__getattribute__() methods forobject,type, andsuper(). Classes inherit this machinery when they derive fromobject or if they have a meta-class providing similar functionality.Likewise, classes can turn-off descriptor invocation by overriding__getattribute__().
Descriptor Example¶
The following code creates a class whose objects are data descriptors whichprint a message for each get or set. Overriding__getattribute__() isalternate approach that could do this for every attribute. However, thisdescriptor is useful for monitoring just a few chosen attributes:
classRevealAccess(object):"""A data descriptor that sets and returns values normally and prints a message logging their access. """def__init__(self,initval=None,name='var'):self.val=initvalself.name=namedef__get__(self,obj,objtype):print('Retrieving',self.name)returnself.valdef__set__(self,obj,val):print('Updating',self.name)self.val=val>>>classMyClass(object):...x=RevealAccess(10,'var "x"')...y=5...>>>m=MyClass()>>>m.xRetrievingvar"x"10>>>m.x=20Updatingvar"x">>>m.xRetrievingvar"x"20>>>m.y5
The protocol is simple and offers exciting possibilities. Several use cases areso common that they have been packaged into individual function calls.Properties, bound methods, static methods, and class methods are allbased on the descriptor protocol.
Properties¶
Callingproperty() is a succinct way of building a data descriptor thattriggers function calls upon access to an attribute. Its signature is:
property(fget=None,fset=None,fdel=None,doc=None)->propertyattribute
The documentation shows a typical use to define a managed attributex:
classC(object):defgetx(self):returnself.__xdefsetx(self,value):self.__x=valuedefdelx(self):delself.__xx=property(getx,setx,delx,"I'm the 'x' property.")
To see howproperty() is implemented in terms of the descriptor protocol,here is a pure Python equivalent:
classProperty(object):"Emulate PyProperty_Type() in Objects/descrobject.c"def__init__(self,fget=None,fset=None,fdel=None,doc=None):self.fget=fgetself.fset=fsetself.fdel=fdelifdocisNoneandfgetisnotNone:doc=fget.__doc__self.__doc__=docdef__get__(self,obj,objtype=None):ifobjisNone:returnselfifself.fgetisNone:raiseAttributeError("unreadable attribute")returnself.fget(obj)def__set__(self,obj,value):ifself.fsetisNone:raiseAttributeError("can't set attribute")self.fset(obj,value)def__delete__(self,obj):ifself.fdelisNone:raiseAttributeError("can't delete attribute")self.fdel(obj)defgetter(self,fget):returntype(self)(fget,self.fset,self.fdel,self.__doc__)defsetter(self,fset):returntype(self)(self.fget,fset,self.fdel,self.__doc__)defdeleter(self,fdel):returntype(self)(self.fget,self.fset,fdel,self.__doc__)
Theproperty() builtin helps whenever a user interface has grantedattribute access and then subsequent changes require the intervention of amethod.
For instance, a spreadsheet class may grant access to a cell value throughCell('b10').value. Subsequent improvements to the program require the cellto be recalculated on every access; however, the programmer does not want toaffect existing client code accessing the attribute directly. The solution isto wrap access to the value attribute in a property data descriptor:
classCell(object):...defgetvalue(self):"Recalculate the cell before returning value"self.recalc()returnself._valuevalue=property(getvalue)
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, methodsare written usingdef orlambda, the usual tools forcreating functions. Methods only differ from regular functions in that thefirst argument is reserved for the object instance. By Python convention, theinstance reference is calledself but may be calledthis or any othervariable name.
To support method calls, functions include the__get__() method forbinding methods during attribute access. This means that all functions arenon-data descriptors which return bound methods when they are invoked from anobject. In pure Python, it works like this:
classFunction(object):...def__get__(self,obj,objtype=None):"Simulate func_descr_get() in Objects/funcobject.c"ifobjisNone:returnselfreturntypes.MethodType(self,obj)
Running the interpreter shows how the function descriptor works in practice:
>>>classD(object):...deff(self,x):...returnx...>>>d=D()# Access through the class dictionary does not invoke __get__.# It just returns the underlying function object.>>>D.__dict__['f']<function D.f at 0x00C45070># Dotted access from a class calls __get__() which just returns# the underlying function unchanged.>>>D.f<function D.f at 0x00C45070># The function has a __qualname__ attribute to support introspection>>>D.f.__qualname__'D.f'# Dotted access from an instance calls __get__() which returns the# function wrapped in a bound method object>>>d.f<bound method D.f of <__main__.D object at 0x00B18C90>># Internally, the bound method stores the underlying function,# the bound instance, and the class of the bound instance.>>>d.f.__func__<function D.f at 0x1012e5ae8>>>>d.f.__self__<__main__.D object at 0x1012e1f98>>>>d.f.__class__<class 'method'>
Static Methods and Class Methods¶
Non-data descriptors provide a simple mechanism for variations on the usualpatterns of binding functions into methods.
To recap, functions have a__get__() method so that they can be convertedto a method when accessed as attributes. The non-data descriptor transforms anobj.f(*args) call intof(obj,*args). Callingklass.f(*args)becomesf(*args).
This chart summarizes the binding and its two most useful variants:
Transformation
Called from anObject
Called from aClass
function
f(obj, *args)
f(*args)
staticmethod
f(*args)
f(*args)
classmethod
f(type(obj), *args)
f(klass, *args)
Static methods return the underlying function without changes. Calling eitherc.f orC.f is the equivalent of a direct lookup intoobject.__getattribute__(c,"f") orobject.__getattribute__(C,"f"). As aresult, the function becomes identically accessible from either an object or aclass.
Good candidates for static methods are methods that do not reference theself variable.
For instance, a statistics package may include a container class forexperimental 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 dependon the data. For instance,erf(x) is handy conversion routine that comes upin 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 orSample.erf(1.5)-->.9332.
Since staticmethods return the underlying function with no changes, the examplecalls are unexciting:
>>>classE(object):...deff(x):...print(x)...f=staticmethod(f)...>>>E.f(3)3>>>E().f(3)3
Using the non-data descriptor protocol, a pure Python version ofstaticmethod() would look like this:
classStaticMethod(object):"Emulate PyStaticMethod_Type() in Objects/funcobject.c"def__init__(self,f):self.f=fdef__get__(self,obj,objtype=None):returnself.f
Unlike static methods, class methods prepend the class reference to theargument list before calling the function. This format is the samefor whether the caller is an object or a class:
>>>classE(object):...deff(klass,x):...returnklass.__name__,x...f=classmethod(f)...>>>print(E.f(3))('E', 3)>>>print(E().f(3))('E', 3)
This behavior is useful whenever the function only needs to have a classreference and does not care about any underlying data. One use for classmethodsis to create alternate class constructors. In Python 2.3, the classmethoddict.fromkeys() creates a new dictionary from a list of keys. The purePython equivalent is:
classDict(object):...deffromkeys(klass,iterable,value=None):"Emulate dict_fromkeys() in Objects/dictobject.c"d=klass()forkeyiniterable:d[key]=valuereturndfromkeys=classmethod(fromkeys)
Now a new dictionary of unique keys can be constructed like this:
>>>Dict.fromkeys('abracadabra'){'a': None, 'r': None, 'b': None, 'c': None, 'd': None}
Using the non-data descriptor protocol, a pure Python version ofclassmethod() would look like this:
classClassMethod(object):"Emulate PyClassMethod_Type() in Objects/funcobject.c"def__init__(self,f):self.f=fdef__get__(self,obj,klass=None):ifklassisNone:klass=type(obj)defnewfunc(*args):returnself.f(klass,*args)returnnewfunc
