Python Forum
super multiple parallel classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
super multiple parallel classes
#1
class a():
    def __init__(self):
        print('this is a')

class b():
    def __init__(self):
        print('this is b')

class c(a,b):
    def __init__(self):
        print('this is c')
        super().__init__()

inc=c()
I only get
'''
this is c
this is a
'''
this is b is skipped.
How do I fix this please? Thank you!
Reply
#2
When you call super().func(...), the first implementation of func() found in the ancestor classes according to the Method Resolution Order is executed. Other implementations are ignored. In your case, a.__init__() is found and b.__init__() is ignored.

You could call the method explicitly
class c(a,b):
    def __init__(self):
        print('this is c')
        super().__init__() # or a.__init__(self)
        b.__init__(self)
Multiple inheritance occurs rarely. When it occurs some classes are usually «mixin» classes and they don't have an __init__ method. So typically, "a" would be a mixin class without such a method and "b" would have an __init__ method, which would be called by super().__init__().

For user defined classes, it is customary to capitalize the class name, so use A, B, C...
Reply
#3
It is important to remember that __init__() is just a method. Other than automatically getting called after an instance is created, it is no different than any other method. In you example __init__() is defined in both classes a and b. When c.__init__() calls super().__init__() which __init__ should it call? Both of them? If so, in what order?

A fix is to use super(). I mean really use super() instead of just partially. If you add super().__init__() to classes a and b, Python uses the MRO to resolve what methods in which order should be called.
class a():
    def __init__(self):
        print('this is a')
        super().__init__()
 
class b():
    def __init__(self):
        print('this is b')
        super().__init__()
 
class c(a,b):
    def __init__(self):
        print('this is c')
        super().__init__()

c()
Output:
this is c this is a this is b
This is the only problem I have with super(). If you use it in one place you really need to use it everywhere for it to work properly in all cases.
catlessness and Gribouillis like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems with super() Hoespilaar 2 271 Apr-10-2024, 10:10 AM
Last Post: Hoespilaar
  super() in class akbarza 1 474 Dec-19-2023, 12:55 PM
Last Post: menator01
  Understanding Python super() for classes OmegaRed94 1 1,840 Jun-09-2021, 09:02 AM
Last Post: buran
  superclass and super() grkiran2011 1 1,751 Jun-20-2020, 04:37 AM
Last Post: deanhystad
  MRO About super() catlessness 1 2,055 Jan-12-2020, 07:54 AM
Last Post: Gribouillis
  Save all values to pandas of multiple classes jenniferruurs 0 1,908 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs
  running multiple commands in a parallel pool Skaperen 6 3,964 Jul-30-2019, 05:49 AM
Last Post: Skaperen
  Super with Sublime Text - TypeError: super() takes at least 1 argument (0 given) Shafla 6 7,476 May-04-2019, 08:30 PM
Last Post: Shafla
  Return Object Created from Multiple Classes emerger 3 3,077 Oct-18-2018, 02:14 AM
Last Post: emerger
  Using classes? Can I just use classes to structure code? muteboy 5 5,076 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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