class A():
def __init__(self):
super().__init__()
print("A")
class B():
def __init__(self):
super().__init__()
print("B")
class C(B,A):
def __init__(self):
super().__init__()
print("C")
c=C()
print(C.mro())
The output of print mro is Class C, Class B, Class A and Class Object. As per that it should come something as C,B,A. But the actual output coming as C,A,B. Why is that? Is it because of super?
What is class
C
's MRO?The Method Resolution Order of a class can be found by getting its
__mro__
attribute. Here is what C's MRO is:However, when creating an instance of class
C
, this is what happens:The actual output, as seen above, is A, B, C, not C, A, B. But this is still not what was expected, and it is different to the MRO.
What is going wrong?
In each class,
super().__init__()
is called beforeprint("...")
is. Therefore, the order that the letters are printed out in effectively gets reversed.Here is a diagram to explain what is happening:
Because the
print()
calls happen after thesuper().__init__()
calls, the letters are printed in reverse order.How to produce the correct result
In order to get the correct the result, the order of the lines within each
__init__()
method must be reversed so that the letter is printed out before the next class'__init__()
is called.The following code would produce the expected output: