Recently I read the official HOW-TO about Python descriptors, which actually derives from an essay written by Raymond Hettinger long time ago. But after reading it for several times, I am still confused about some parts of it. I will quote some paragraphs, followed by my confusions and questions.
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.
- Is this the same with class? What is the precedence chain of a class if its dictionary has an entry with the same name as a data/non-data descriptor?
For objects, the machinery is in
object.__getattribute__()
which transformsb.x
intotype(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.For classes, the machinery is in
type.__getattribute__()
which transformsB.x
intoB.__dict__['x'].__get__(None, B)
.
- The above two paragraphs tell the process of a descriptor's being invoked automatically upon attribute access. It lists the difference between attribute's being accessed by an instance (
b.x
) and a class (B.x
). However, here are my confusions:- if the attribute of a class or an instance is not a descriptor, will the transformation (i.e., transforms
b.x
intotype(b).__dict__['x'].__get__(b, type(b))
andB.x
intoB.__dict__['x'].__get__(None, B)
) still proceed? Is returning the attribute in this class's or instance's dict directly simpler? - If an instance's dictionary has an entry with the same name as a non-data descriptor, according to the precedence rule in the first quote, the dictionary entry takes precedence, at this time will the transformation still proceed? Or just return the value in its dict?
- if the attribute of a class or an instance is not a descriptor, will the transformation (i.e., transforms
Non-data descriptors provide a simple mechanism for variations on the usual patterns of binding functions into methods.
- Is non-data descriptors chosen because functions/methods can only be gotten, but cannot be set?
- What's the underlying mechanism for binding functions into methods? Since class dictionaries store methods as functions, if we call the same method using a class and its instance respectively, how can the underlying function tell whether its first argument should be self or not?
Functions have a
__get__()
method so that they can be converted to a method when accessed as attributes. The non-data descriptor transforms aobj.f(*args)
call intof(obj, *args)
. Callingklass.f(*args)
becomes f(*args).
- How can a non-data descriptor transform a
obj.f(*args)
call intof(obj, *args)
? - How can a non-data descriptor transform a
klass.f(*args)
call intof(*args)
? - What's the underlying mechanism of the above two transformations? Why do differences exist between class and instance?
- What does the role
__get__()
method play under the above circumstance?