Sub topic 5
Data and non-data descriptors
This chapter assumes you know how __get__, __set__ and __delete__ are defined, and that you know an instance has its own __dict__ separate from its class. Here you will learn the one rule that decides whether a descriptor on a class beats an entry of the same name in the instance.
The dividing line
Python sorts descriptors by which methods their class defines:
| Kind | Methods defined on the descriptor's class |
|---|---|
| Non-data descriptor | `__get__` only |
| Data descriptor | `__get__` plus `__set__` or `__delete__` (or both) |
The check looks at the type of the descriptor object, not at the object itself. An object with no __get__ is not a descriptor at all, even if it has __set__.
Why the difference matters
When you read obj.name, Python first finds name on the class through the MRO. If that class attribute is a data descriptor, Python calls its __get__ immediately and never looks in obj.__dict__. If it is a non-data descriptor, Python checks obj.__dict__ first, and the instance entry wins when one exists.
class DataDesc:
def __get__(self, obj, objtype=None):
return "data descriptor"
def __set__(self, obj, value):
print("__set__ called")
class NonDataDesc:
def __get__(self, obj, objtype=None):
return "non-data descriptor"
class A:
d = DataDesc()
n = NonDataDesc()
a = A()
a.__dict__["d"] = "instance d"
a.__dict__["n"] = "instance n"
print(a.d) # data descriptor
print(a.n) # instance nBoth names have an entry in the instance __dict__. The data descriptor d ignores it. The non-data descriptor n is shadowed by it.
The same rule for assignment
Assignment follows a matching rule. obj.name = value calls __set__ if the class attribute is a data descriptor. Otherwise Python writes directly into obj.__dict__. This is why the instance can shadow a non-data descriptor: assigning to the name simply creates an instance entry that hides it.
a = A()
a.d = 5 # prints "__set__ called"
print(a.__dict__) # {} because the descriptor handled the write
a.n = 5 # no __set__ exists, so Python writes to the instance
print(a.n) # 5Built-in examples
The rule explains several behaviors you have already seen:
propertydefines__set__(and__delete__) even when you give it no setter. The missing setter makes__set__raiseAttributeError. That is why you cannot override a read-only property by assigning to it.- Functions define only
__get__. They are non-data descriptors, soobj.method = somethingreplaces the method for that one instance without touching the class. functools.cached_propertydefines only__get__. It stores its result in the instance__dict__under the same name. Later reads find that entry first and skip the descriptor.
Checking which kind you have
You can test this from the type, which mirrors what the interpreter does:
def kind(attr):
t = type(attr)
if not hasattr(t, "__get__"):
return "not a descriptor"
if hasattr(t, "__set__") or hasattr(t, "__delete__"):
return "data descriptor"
return "non-data descriptor"
class C:
def f(self): pass
p = property(lambda self: 1)
print(kind(C.__dict__["f"])) # non-data descriptor
print(kind(C.__dict__["p"])) # data descriptor
print(kind(42)) # not a descriptorExercise
A class defines x = Desc(), where Desc has __get__ and __delete__ but no __set__. An instance's __dict__ contains "x": 7. What does obj.x return, and why?
Show answer
It calls Desc.__get__. Defining __delete__ is enough to make Desc a data descriptor, so it takes precedence over the instance __dict__ entry and the value 7 is ignored on reads.