class parent:
def __init__(self):
self.a=2
self.b=4
def form1(self):
print("calling parent from1")
print('p',self.a+self.b)
class child1(parent):
def __init__(self):
self.a=50
self.b=4
def form1(self):
print('bye',self.a-self.b)
def callchildform1(self):
print("calling parent from child1")
super().form1()
class child2(parent):
def __init__(self):
self.a=3
self.b=4
def form1(self):
print('hi',self.a*self.b)
def callchildform1(self):
print("calling parent from child2")
super().form1()
class grandchild(child1,child2):
def __init__(self):
self.a=10
self.b=4
def callingparent(self):
super().form1()
g=grandchild()
g.callchildform1()
In the above code, when I call g.callchildform1()
, according to MRO rule, the method will be first searched in the same class, then the first parent (child1 here) and then the second parent (child2).
As expected, it calls child1.callchildform1()
and executes the first line print("calling parent from child1")
. But after this, I expected that the next line super().form1()
will be executed which will call parent.form1()
but this doesnt happen. Instead, child2.form1()
is being called. Please explain why this happens?