I have 2 classes: A (which needs 1 argument to initialize) and B (which needs 2 arguments to initialize), and a third class C which derives from both A and B.
class A:
def __init__(self, sval):
print("A: rcd value: ", sval)
self.aval = sval
class B:
def __init__(self, sval, tval):
print("B: rcd 2 values: ", sval, tval)
self.aval=sval
self.bval=tval
class C(A,B):
def __init__(self, a, b, c):
super().__init__(a)
super().__init__(b,c) # error here
c = C(1,2,3)
When I run above code, there is error at the last line; __init__ of class A is called, not that of class B.
A: rcd value: 1
Traceback (most recent call last):
File "inheritance.py", line 20, in <module>
c = C(1,2,3)
File "inheritance.py", line 16, in __init__
super().__init__(b,c)
TypeError: __init__() takes 2 positional arguments but 3 were given
How can I call __init__ functions of both A and B from __init__ of class C?
Edit: I am using Python 3.5.3 on Debian Linux, though I will prefer a solution which works on both Python2 and Python3.
Python-2.xandPython-3.xtag as you never specified which version of Python you were using, and the solution works for bothAandBhave attributes namedaval; one needs to be renamed. If you don't have control overAorB, then you're going to have to define an adaptor for one of them.superis to have bothAandBuse it as well, then usesuperonce fromC.__init__.