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.
2 Answers 2
If you continue to read, then the documentation says "In fact, it is slightly more complex than that". The thing you mention is oriented to single inheritance language. Python works different, and they implemented super(). You can either read on the documentation to know how does it work, or/and (depends on your level of curiosity) you can go read the answer on here and start playing with the code and see what happens.
Good Luck
Comments
Attribute lookup occurs according to the method resolution order (MRO) established when the class is first defined. One important rule of the MRO is that no class appears before any of its children. This means the MRO for D is [D, B, C, A, object], not [D, B, A, object, C] as you would expect from a pure depth-first search.
Comments
Explore related questions
See similar questions with these tags.
Ccomes beforeAin the resolution order? If so, the answer is that the order isn't just a depth first search in Python 3. (Hence the hedging: "For most purposes, in the simplest cases, you can think of..." Also hence the next line after your quote: "In fact, it is slightly more complex than that...")