Text of peps.python.org › pep-0447 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

PEP 447 – Add __getdescriptor__ method to metaclass | peps.python.org

PEP 447 – Add __getdescriptor__ method to metaclass PEP 447 – Add __getdescriptor__ method to metaclass Author : Ronald Oussoren <ronaldoussoren at mac.com> Status : Deferred Type : Standards Track Created : 12-Jun-2013 Post-History : 02-Jul-2013, 15-Jul-2013, 29-Jul-2013, 22-Jul-2015 Table of Contents Abstract PEP Status Rationale Background The superclass attribute lookup hook Aside: Attribute resolution algorithm in Python In Python code Example usage In C code Use of this hook by the interpreter Other changes to the implementation Impact of this PEP on introspection Performance impact Micro benchmarks Pybench Alternative proposals __getattribute_super__ Reuse tp_getattro Alternative placement of the new method History Discussion threads References Copyright Abstract Currently object.__getattribute__ and super.__getattribute__ peek in the __dict__ of classes on the MRO for a class when looking for an attribute.

This PEP adds an optional __getdescriptor__ method to a metaclass that replaces this behavior and gives more control over attribute lookup, especially when using a super object. That is, the MRO walking loop in _PyType_Lookup and super.__getattribute__ gets changed from: def lookup ( mro_list , name ): for cls in mro_list : if name in cls . __dict__ : return cls . __dict__ return NotFound to: def lookup ( mro_list , name ): for cls in mro_list : try : return cls . __getdescriptor__ ( name ) except AttributeError : pass return NotFound The default implementation of __getdescriptor__ looks in the class dictionary: class type : def __getdescriptor__ ( cls , name ): try : return cls . __dict__ [ name ] except KeyError : raise AttributeError ( name ) from None PEP Status This PEP is deferred until someone has time to update this PEP and push it forward.

Rationale It is currently not possible to influence how the super class looks up attributes (that is, super.__getattribute__ unconditionally peeks in the class __dict__ ), and that can be problematic for dynamic classes that can grow new methods on demand, for example dynamic proxy classes. The __getdescriptor__ method makes it possible to dynamically add attributes even when looking them up using the super class . The new method affects object.__getattribute__ (and PyObject_GenericGetAttr ) as well for consistency and to have a single place to implement dynamic attribute resolution for classes.

Background The current behavior of super.__getattribute__ causes problems for classes that are dynamic proxies for other (non-Python) classes or types, an example of which is PyObjC . PyObjC creates a Python class for every class in the Objective-C runtime, and looks up methods in the Objective-C runtime when they are used. This works fine for normal access, but doesn’t work for access with super objects. Because of this PyObjC currently includes a custom super that must be used with its classes, as well as completely reimplementing PyObject_GenericGetAttr for normal attribute access.

The API in this PEP makes it possible to remove the custom super and simplifies the implementation because the custom lookup behavior can be added in a central location. Note PyObjC cannot precalculate the contents of the class __dict__ because Objective-C classes can grow new methods at runtime. Furthermore, Objective-C classes tend to contain a lot of methods while most Python code will only use a small subset of them, this makes precalculating unnecessarily expensive.

The superclass attribute lookup hook Both super.__getattribute__ and object.__getattribute__ (or PyObject_GenericGetAttr and in particular _PyType_Lookup in C code) walk an object’s MRO and currently peek in the class’ __dict__ to look up attributes. With this proposal both lookup methods no longer peek in the class __dict__ but call the special method __getdescriptor__ , which is a slot defined on the metaclass. The default implementation of that method looks up the name the class __dict__ , which means that attribute lookup is unchanged unless a metatype actually defines the new special method.

Aside: Attribute resolution algorithm in Python The attribute resolution process as implemented by object.__getattribute__ (or PyObject_GenericGetAttr in CPython’s implementation) is fairly straightforward, but not entirely so without reading C code. The current CPython implementation of object.__getattribute__ is basically equivalent to the following (pseudo-) Python code (excluding some house keeping and speed tricks): def _PyType_Lookup ( tp , name ): mro = tp . mro () assert isinstance ( mro , tuple ) for base in mro : assert isinstance ( base , type ) # PEP 447 will change these lines: try : return base . __dict__ [ name ] except KeyError : pass return None class object : def __getattribute__ ( self , name ): assert isinstance ( name , str ) tp = type ( self ) descr = _PyType_Lookup ( tp , name ) f = None if descr is not None : f = descr . __get__ if f is not None and descr . __set__ is not None : # Data descriptor return f ( descr , self , type ( self )) dict = self . __dict__ if dict is not None : try : return self . __dict__ [ name ] except KeyError : pass if f is not None : # Non-data descriptor return f ( descr , self , type ( self )) if descr is not None : # Regular class attribute return descr raise AttributeError ( name ) class super : def __getattribute__ ( self , name ): assert isinstance ( name , unicode ) if name != '__class__' : starttype = self . __self_type__ mro = startype . mro () try : idx = mro . index ( self . __thisclass__ ) except ValueError : pass else : for base in mro [ idx + 1 :]: # PEP 447 will change these lines: try : descr = base . __dict__ [ name ] except KeyError : continue f = descr . __get__ if f is not None : return f ( descr , None if ( self . __self__ is self . __self_type__ ) else self . __self__ , starttype ) else : return descr return object . __getattribute__ ( self , name ) This PEP should change the dict lookup at the lines starting at “# PEP 447” with a method call to perform the actual lookup, making is possible to affect that lookup both for normal attribute access and access through the super proxy .

Note that specific classes can already completely override the default behaviour by implementing their own __getattribute__ slot (with or without calling the super class implementation). In Python code A meta type can define a method __getdescriptor__ that is called during attribute resolution by both super.__getattribute__ and object.__getattribute : class MetaType ( type ): def __getdescriptor__ ( cls , name ): try : return cls . __dict__ [ name ] except KeyError : raise AttributeError ( name ) from None The __getdescriptor__ method has as its arguments a class (which is an instance of the meta type) and the name of the attribute that is looked up.

It should return the value of the attribute without invoking descriptors, and should raise AttributeError when the name cannot be found. The type class provides a default implementation for __getdescriptor__ , that looks up the name in the class dictionary. Example usage The code below implements a silly metaclass that redirects attribute lookup to uppercase versions of names: class UpperCaseAccess ( type ): def __getdescriptor__ ( cls , name ): try : return cls . __dict__ [ name . upper ()] except KeyError : raise AttributeError ( name ) from None class SillyObject ( metaclass = UpperCaseAccess ): def m ( self ): return 42 def M ( self ): return "fortytwo" obj = SillyObject () assert obj . m () == "fortytwo" As mentioned earlier in this PEP a more realistic use case of this functionality is a __getdescriptor__ method that dynamically populates the class __dict__ based on attribute access, primarily when it is not possible to reliably keep the class dict in sync with its source, for example because the source used to populate __dict__ is dynamic as well and does not have triggers that can be used to detect changes to that source.

An example of that are th

The rest of this page is on the live site.