0
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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    please read: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ – juanpa.arrivillaga Jan 14 '22 at 15:34
  • 1
    Rule number one of using `super`: it supports *cooperative* inheritance, which means *all* classes in the hierarchy need to use it correctly. That means `Parent1` and `Parent2` need to use it as well. `super` is misnamed; it does not always refer to a parent of the class you are writing, but rather the *next* class in the method resolution order of the object calling the method. – chepner Jan 14 '22 at 15:39

0 Answers0