I'm trying to use multiple inheritance to add some functionality to one of the existing classes that I have. The problem is that this new class and my current base class have different arguments in their constructors. Namely the new class has 1 extra argument. After some googling I understood that I can add **kwargs to the current base class(the one with one argument less). Example:
class A(object):
def __init__(self, a):
print('__init__', locals())
class B(A):
def __init__(self, a, b):
super(B, self).__init__(a)
print('__init__', locals())
class C(B):
def __init__(self, a, b):
super(C, self).__init__(a, b)
print('__init__', locals())
class D(C):
def __init__(self, a, b):
super(D, self).__init__(a, b)
print('__init__', locals())
class E(D):
def __init__(self, a, b, *args, **kwargs):
super(E, self).__init__(a, b)
print('__init__', locals())
class F(C):
def __init__(self, a, b):
super(F, self).__init__(a, b)
print('__init__', locals())
class G(F):
def __init__(self, a, b, c):
super(G, self).__init__(a, b)
print('__init__', locals())
class H(G):
def __init__(self, a, b, c):
super(H, self).__init__(a, b, c)
print('__init__', locals())
class I(E, H):
def __init__(self, a, b, c):
super(I, self).__init__(a, b, c=c)
print('__init__', locals())
for c in I.__mro__:
print(c)
I(0, 1, 2)
But I get this error:
<class '__main__.I'>
<class '__main__.E'>
<class '__main__.D'>
<class '__main__.H'>
<class '__main__.G'>
<class '__main__.F'>
<class '__main__.C'>
<class '__main__.B'>
<class '__main__.A'>
<class 'object'>
Traceback (most recent call last):
File "/tmp/c.py", line 58, in <module>
I(0,1,2)
File "/tmp/c.py", line 50, in __init__
super(I, self).__init__(a, b, c=c)
File "/tmp/c.py", line 26, in __init__
super(E, self).__init__(a, b)
File "/tmp/c.py", line 20, in __init__
super(D, self).__init__(a, b)
TypeError: __init__() missing 1 required positional argument: 'c'