Feb-01-2024, 12:57 PM
Quote:As in the docstring of this code has been written, this subject has been discussed in the given address, but the issue is ambiguous to me still.What part confuses you? Maybe an example with more depth will help?
class A:
pass
class B:
pass
class C:
pass
class D:
pass
class E(A, B):
pass
class F(C, D):
pass
class G(E, F):
pass
print(*G.__mro__, sep="\n")
Output:<class '__main__.G'>
<class '__main__.E'>
<class '__main__.A'>
<class '__main__.B'>
<class '__main__.F'>
<class '__main__.C'>
<class '__main__.D'>
<class 'object'>
Which we can draw like this:Output:A B C D
\ / \ /
E F
\ /
G
We traverse the mro graph giving preference the left node (first superclass). If we don't find what we need in the left branch, back up one level and try the next superclass. So if we cannot find the method we are looking for in A, back up to E and try the next superclass of E which is B. If we can't find it there we have to back up to G and check the next superclass, F. The search order is Look local, then look at leftmost super. When you reach object, back up and look at next super.In practice, using inheritance like this will quickly drive you insane, and multiple inheritance examples in Python are rare compared to single inheritance. But the mechanism is in place for you to drive yourself insane if that's what you want to do.