class Parent1():
def __init__(self, name1):
self.name1 = name1
def show_parent1(self):
print(f"The name of parent1 is {self.name1}")
class Parent2():
def __init__(self,name2):
self.name2 = name2
def show_parent2(self):
print(f"The name of parent2 is {self.name2}")
class child(Parent1, Parent2):
def __init__(self,name1, name2, name3):
super().__init__(name1)
super().__init__(name2)
self.name3 = name3
def child_name(self):
print(f"The name of the child is {self.name3}")
c1 = child("P1", "P2", "C")
c1.show_parent1()
c1.show_parent2()
c1.child_name()
My output is
The name of parent1 is P2
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15220/2812189334.py in <module>
1 c1 = child("P1", "P2", "C")
2 c1.show_parent1()
----> 3 c1.show_parent2()
4
5 c1.child_name()
~\AppData\Local\Temp/ipykernel_15220/2311005546.py in show_parent2(self)
4
5 def show_parent2(self):
----> 6 print(f"The name of parent2 is {self.name2}")
AttributeError: 'child' object has no attribute 'name2'
How to override __init__
method in multiple inheritance?
I can easily override the __init__
method for single inheritance but was not able to do it for this.