In accordance with the official documentation about classes in Python, we see in the item of multiple inheritance the definition:
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 is an overlap in the Hierarchy.
That is, using your example, if a method/attribute is not found in the class D
, Python will search by its definition in the class C
, first on the left. If not found, would go deeper, for the basic classes of C
. Since he doesn’t own it, he goes back to class B
and if you don’t, go to class A
, if not, for the basic classes of A
. If you still can’t find the definition, fire the exception.
Therefore, when executed var.y()
, the definition of y
in D, C, B, A
, in this order. You find in C
, therefore returns C.y
. When executed var.get_c()
, the same thing, is also found in C
, returning the value of self.c
, however, what happens here is that in the class initializer D
, you run the class initialize B
after the class C
and in both the property self.c
is defined. That is, when executed C.__init__()
, the value of self.c
will be 5, but when executed B.__init__()
, the value of self.c
becomes 16, which explains why var.get_c()
return the value 16-