Short:
You can't and you don't need to use super()
to call __init__
with different form. Of course there is a way to do, but I do not recommend that.
Long:
Python uses the list of classes to inherit from. You can see that list with
ClassName.__mro__
. mro
stands for Method Resolution Order
Think about you have three classes, ClassA
, ClassB
, ClassC
and ClassC
extends ClassA
, and ClassB
(If you are using python2, ClassA
and ClassB
musts extend from object
)
class ClassC(ClassA, ClassB):
Then, ClassC.__mro__
will be like below:
(<class '__main__.ClassC'>, <class '__main__.ClassA'>, <class '__main__.ClassB'>, <class 'object'>)
So python inherits ClassC
, ClassA
, ClassB
order. And it assumes that methods have same form in this list. This is why you can't use different forms of __init__
call using super()
.
If you need to use super()
and there is no option, you can consider passing argument as **kwargs
.
class ClassA():
def __init__(self, **kwargs):
super(ClassA, self).__init__(**kwargs)
print("Class A Initialized:", kwargs['a'])
class ClassB():
def __init__(self, **kwargs):
# super(ClassA, self).__init__(**kwargs)
# if you call above, you'll get type error, because above will call object.__init__ which takes only 1 argument
print("Class B Initialized:", kwargs['b'])
class ClassC(ClassA, ClassB):
def __init__(self, **kwargs):
super(ClassC, self).__init__(**kwargs)
print("Class C Initialized:", kwargs['c'])
ClassC(a="A", b="B", c="C")
And you can get below output :
Class B Initialized: B
Class A Initialized: A
Class C Initialized: C
But still, I recommend that do not use this form of inheritance, because it is very complicated