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?
No, if an attribute is defined both in a superclass and a subclass, the superclass value is completely ignored.
if the attribute of a class or an instance is not a descriptor, will the transformation (i.e., transforms b.x into
type(b).__dict__['x'].__get__(b, type(b))
and B.x intoB.__dict__['x'].__get__(None, B))
still proceed?
No, it returns directly the object gotten from the class's __dict__
. Or equivalently, yes, if you pretend that all objects have by default a method __get__()
that ignores its arguments and returns self
.
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?
What is not clear in the paragraph that you quoted (maybe it's written down elsewhere) is that when b.x
decides to return b.__dict__['x']
, no __get__
is invoked in any case. The __get__
is invoked exactly when the syntax b.x
or B.x
decides to return an object that lives in a class dict.
Is non-data descriptors chosen because functions/methods can only be gotten, but cannot be set?
Yes: they are a generalization of the "old-style" class model in Python, in which you can say B.f = 42
even if f
is a function object living in the class B. This lets the function object be overriden with an unrelated object. The data descriptors on the other hand have a different logic in order to support property
.
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?
To understand this, you need to have "method objects" in mind. The syntax b.f(*args)
is equivalent to (b.f)(*args)
, which is two steps. The first step calls f.__get__(b)
; this returns a method object that stores both b and f. The second step calls the method object, which will in turn call the original f by adding b as extra argument. This is something which doesn't occur for B.f
, simply because B.f
, i.e. f.__get__(None, B)
, is just f (in Python 3). It's the way the special method __get__
is designed on function objects.