With the file super5.py:
class A:
def m(self):
print("m of A called")
class B(A):
def m(self):
print("m of B called")
super().m()
class C(A):
def m(self):
print("m of C called")
super().m()
class D(B,C):
def m(self):
print("m of D called")
super().m()
we can do the following:
>>> from super5 import D
>>> x = D()
>>> x.m()
m of D called
m of B called
m of C called
m of A called
To me, this doesn't make sense, because when I execute x.m()
, I expect the following to happen:
- The first line of
m
ofD
is executed and thus"m of D called"
is output. - The second line,
super().m()
is executed, which first takes us tom
ofB
. - In
m
ofB
,"m of B called"
is first output, and then,m
ofA
is executed due to thesuper.m()
call inm
ofB
, and"m of A called"
is output. m
ofC
is executed in a fashion analogous to 3.
As you can see, what I expect to see is:
m of D called
m of B called
m of A called
m of C called
m of A called
Why am I wrong? Is python somehow keeping track of the number of super()
calls to a particular superclass and limiting the execution to 1?