What Is Happening Here In Python With Oops While Trying Diamond Shape Problem
I am learning OOPs with python. created below code to replicate the diamond shape problem in multiple inheritence. I am running the below code in jupyter notebook and output is gen
Solution 1:
You may find the __mro__
attribute of a class informative. Here, MRO stands for Method Resolution Order.
Consider this modification to your code:
classParent:
def__init__(self):
self.a = 2
self.b = 4defprint_name(self):
print("parent")
defform1(self):
print("calling parent form1")
print('p', self.a + self.b)
classChild1(Parent):
def__init__(self):
self.a = 50
self.b = 4defprint_name(self):
print("child1")
defprint_super_name(self):
super().print_name()
defform1(self):
print('bye', self.a - self.b)
defcallchildform1(self):
print("calling parent from child1")
super().form1()
classChild2(Parent):
def__init__(self):
self.a = 3
self.b = 4defprint_name(self):
print("child2")
defform1(self):
print('hi', self.a * self.b)
defcallchildform1(self):
print("calling parent from child2")
super().form1()
classGrandchild(Child1, Child2):
def__init__(self):
self.a = 10
self.b = 4defprint_name(self):
print("grandchild")
defprint_super_name(self):
super().print_name()
defprint_super_super_name(self):
super().print_super_name()
defcallingparent(self):
super().form1()
g = Grandchild()
print("When I print the name of my class it is:")
g.print_name()
print("When I print my superclass name, it is:")
g.print_super_name()
print("When I print the name of the superclass of my superclass, it is:")
g.print_super_super_name()
print("When you call methods on me, they will be executed from my class and my parent classes in the following order:")
print(Grandchild.__mro__)
g.form1()
g.callchildform1()
g.callingparent()
The output is:
When I print the name of my classitis:
grandchild
When I print my superclassname, it is:
child1
When I print the name of the superclassof my superclass, it is:
child2
When you call methods on me, they will be executed from my classand my parent classes in the following order:
(<class'__main__.Grandchild'>, <class'__main__.Child1'>, <class'__main__.Child2'>, <class'__main__.Parent'>, <class'object'>)
bye 6
calling parent from child1
hi 40
bye 6
When you run g.callchildform1()
Python looks for the definition of callchildform1
in Grandchild
. It isn't there, so the next place it looks is Child1
. You can see from the example and from the method resolution order that when an instance of Grandchild
calls a method defined in Child1
which calls super()
, the search for the called method will begin in Child2
.
Post a Comment for "What Is Happening Here In Python With Oops While Trying Diamond Shape Problem"