I read Python's documentation and can't understand this piece of information
class DerivedClassName(Base1, Base2, Base3): <statement-1> . . . <statement-N>
For most purposes, in the simplest cases, you can think of the search for attributes inherited from a parent class as depth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy. Thus, if an attribute is not found in
DerivedClassName
, it is searched for inBase1
, then (recursively) in the base classes ofBase1
, and if it was not found there, it was searched for inBase2
, and so on.
If it is new-style class, why does Python search recursively in the base classes of Base1 and not going to Base2 then Base3?
class A(object):
attr = 1
class B(A):
pass
class C(A):
attr = 2
class D(B,C):
pass
x = D()
print(x.attr)# 2
Sample from Mark Lutz's book. Python goes to D then B then C.