Reputation: 615
I am learning inheritance and got this problem
class A:
def test(self):
print("test of A called")
class B(A):
def test(self):
print("test of B called")
super().test()
class C(A):
def test(self):
print("test of C called")
super().test()
class D(B,C):
def test2(self):
print("test of D called")
obj=D()
obj.test()
The output is below according to the website where this ques was posted
test of B called
test of C called
test of A called
But in my opinion, the output should be
test of B called
test of A called
Because, class B will be called first (Acc to MRO), and then super().test()
is called from class B which will print
test of A called
.
Where am I wrong here?
Upvotes: 2
Views: 84
Reputation: 49
Paste your code her [http://pythontutor.com/visualize.html][1] and Visulize line by line. I hope its help.
Upvotes: 0
Reputation: 5564
Short answer: When B.test
calls super().test()
, it uses the MRO of the original object. It doesn't just look at B
's hierarchy.
Upvotes: 3