Sub topic 6
The complete lookup order
This chapter assumes you know how instance and class __dict__ namespaces work, how the method resolution order (MRO) is built, and what __get__, __set__ and __delete__ do. Here they are combined into one algorithm.
The four steps
When you write obj.name, Python calls type(obj).__getattribute__(obj, "name"). For ordinary classes that is object.__getattribute__, and it works like this:
- Search
type(obj).__mro__fornameand keep the first match. Call it the class attribute. - If the class attribute is a data descriptor (it defines
__set__or__delete__), return its__get__(obj, type(obj))result. The instance is never consulted. - Otherwise, if
nameis inobj.__dict__, return that value. - Otherwise, if a class attribute was found, return
__get__(obj, type(obj))when it is a non-data descriptor. If it is a plain value, return it unchanged.
If every step fails, __getattribute__ raises AttributeError. Python then calls __getattr__ if the class defines it.
Watching each branch
The class below has one attribute for each branch. The instance __dict__ is filled directly so every name has an instance value that competes with the class.
class Data:
def __get__(self, obj, objtype=None):
return "data descriptor"
def __set__(self, obj, value):
pass
class NonData:
def __get__(self, obj, objtype=None):
return "non-data descriptor"
class A:
d = Data()
n = NonData()
plain = "class attribute"
def __getattr__(self, name):
return f"__getattr__({name})"
a = A()
a.__dict__.update(d="inst d", n="inst n", plain="inst plain")
print(a.d) # data descriptor
print(a.n) # inst n
print(a.plain) # inst plain
del a.__dict__["plain"]
print(a.plain) # class attribute
print(a.missing) # __getattr__(missing)a.d ignores the instance value, because step 2 comes before step 3. a.n returns the instance value, because the non-data descriptor is only reached in step 4. After the instance entry for plain is removed, the class value shows through.
Summary of outcomes
| Class attribute found | In `obj.__dict__` | Result of `obj.name` |
|---|---|---|
| Data descriptor | yes or no | Descriptor's `__get__` |
| Non-data descriptor | yes | Instance value |
| Non-data descriptor | no | Descriptor's `__get__` |
| Plain value | yes | Instance value |
| Plain value | no | Class value |
| None | yes | Instance value |
| None | no | `AttributeError`, then `__getattr__` |
Details that cause surprises
__getattr__ is a fallback, not a hook on every access. It runs only after __getattribute__ raises AttributeError. This includes an AttributeError raised inside a descriptor's __get__. A buggy property can therefore be hidden by a __getattr__ that returns something else.
Assignment has its own, shorter rule. obj.name = value checks the class for a data descriptor and calls its __set__. If there is none, it writes to obj.__dict__. A non-data descriptor never intercepts assignment, so assigning to it creates an instance entry that shadows it from then on.
Exercise
A class defines x as a non-data descriptor whose __get__ returns 1. You run obj = C(); obj.x = 5; print(obj.x); del obj.x; print(obj.x). What is printed, and why?
Show answer
It prints 5, then 1. Assignment finds no data descriptor, so 5 is stored in obj.__dict__. On the first read, the instance entry beats the non-data descriptor (step 3 before step 4). del obj.x removes the instance entry, so the second read falls through to the descriptor's __get__, which returns 1.