Python Forum
Multiple Inheritance using super()
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Inheritance using super()
#1
Hi All,
I was trying to understand Inheritance concept in Python.
When I wrote the code without using super(), I was getting desired results. You can see that below
class Base1(object):
    def __init__(self):
        self.name1 = "Peter"

    def showName1(self):
        print(self.name1)

class Base2(object):
    def __init__(self):
        self.name2 = "John"

    def showName2(self):
        print(self.name2)

class Child(Base2, Base1):
    def __init__(self):
        self.name3 = "Sagar"
        Base1.__init__(self)
        Base2.__init__(self)
        

    def showName3(self):
        print(self.name3)

obj = Child()
obj.showName1()
obj.showName2()
obj.showName3()
Output:
Peter John Sagar
But when I wrote super() in place of writing class name in Child class's __init__() function,
class Base1(object):
    def __init__(self):
        self.name1 = "Peter"

    def showName1(self):
        print(self.name1)

class Base2(object):
    def __init__(self):
        self.name2 = "John"

    def showName2(self):
        print(self.name2)

class Child(Base2, Base1):
    def __init__(self):
        self.name3 = "Sagar"
        super(Child,self).__init__()
        

    def showName3(self):
        print(self.name3)

obj = Child()
obj.showName1()
obj.showName2()
obj.showName3()

I am getting following error:
Error:
Traceback (most recent call last): File "D:\Sagar\Python Related\Scripts\MultipleInheritance.py", line 25, in <module> obj.showName1() File "D:\Sagar\Python Related\Scripts\MultipleInheritance.py", line 6, in showName1 print(self.name1) AttributeError: 'Child' object has no attribute 'name1'
Is super() not used in case of Multiple Inheritance ?
Reply
#2
(Sep-06-2017, 08:56 AM)Sagar Wrote: Is super() not used in case of Multiple Inheritance ?
I think the issue is all classes in the chain need to make the call to super, even if you think they are the last call.
You should probably watch this video if you have not done so:  
Super considered super
Reply
#3
I was studying super() and its calling sequence of the Base and Sibling classes. I found that super is called in anticlockwise order and stops calling the next sibling or parent if there's no super() found in current class. I am sharing, what I found. Please correct me, if I am wrong.
Consider the following code
class A(object):
    def foo(self):
        print('A')

class B(A):
    def foo(self):
        print('B')
        super().foo()
 
class C(A):
    def foo(self):
        print('C')
        super().foo()
 
class D(B,C):
    def foo(self):
        print('D')
        super().foo()
 
d = D()
d.foo()
Here D inherits from the classes B and C and class A is the Base class of both classes B and C.
The output given by the above code is as follows:
Output:
D B C A
Now I included another class called "Foreign" in between and without super() like this:
class A(object):
    def foo(self):
        print('A')

class Foreign(object):
    def foo(self):
        print('Foreign')
        #super().foo()

class B(Foreign):
    def foo(self):
        print('B')
        super().foo()
 
class C(A):
    def foo(self):
        print('C')
        super().foo()
 
class D(B,C):
    def foo(self):
        print('D')
        super().foo()
 
d = D()
d.foo()
and the output for the above was:
Output:
D B Foreign
Once I un-commented the super() of Foreign Class, the output was like this:
Output:
D B Foreign C A
If I change the order of Base classes in the class definition of class D that is:
from
class D(B,C):
to
class D(C,B):
the order of calling super() also changes.
Note: There is no super in class A.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with super() Hoespilaar 2 220 Apr-10-2024, 10:10 AM
Last Post: Hoespilaar
  super() and order of running method in class inheritance akbarza 7 723 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  super() in class akbarza 1 449 Dec-19-2023, 12:55 PM
Last Post: menator01
  super multiple parallel classes catlessness 2 1,327 Jun-07-2022, 02:35 PM
Last Post: deanhystad
  superclass and super() grkiran2011 1 1,725 Jun-20-2020, 04:37 AM
Last Post: deanhystad
  MRO About super() catlessness 1 2,028 Jan-12-2020, 07:54 AM
Last Post: Gribouillis
  Multiple Inheritance - Help pls! magnusbrigido 1 1,832 May-17-2019, 12:56 PM
Last Post: ichabod801
  Super with Sublime Text - TypeError: super() takes at least 1 argument (0 given) Shafla 6 7,396 May-04-2019, 08:30 PM
Last Post: Shafla
  Multiple inheritance - the right way ? denis_beurive 6 4,608 Feb-14-2019, 09:24 AM
Last Post: denis_beurive

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020