Reputation: 2537
Can anyone explain why I am getting __init__()
takes exactly 1 argument error with the below code
class ABC(object):
def __init__(self, **kwargs):
self.x=20
super(ABC, self).__init__(self, **kwargs)
class DEF(object):
def __init__(self, **kwargs):
self.y=30
super(DEF, self).__init__(self, **kwargs)
class XYZ(ABC,DEF):
def __init__(self, **kwargs):
super(XYZ, self).__init__(self, **kwargs)
>>> x=XYZ()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/test.py", line 13, in __init__
super(XYZ, self).__init__(self, **kwargs)
TypeError: __init__() takes exactly 1 argument (2 given)
>>> x=XYZ(a=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/hprem/junk", line 13, in __init__
super(XYZ, self).__init__(self, **kwargs)
TypeError: __init__() takes exactly 1 argument (3 given)
Upvotes: 0
Views: 4543
Reputation: 251365
You don't need to pass self
explicitly to the superclass __init__
. By doing super(XYZ, self)
you created a "bound" super object that "knows" what self
is. Just do super(XYZ, self).__init__(**kwargs)
(and likewise for your other calls).
Note, though, that this class structure will fail if you pass any kwargs. object.__init__
doesn't accept any parameters, so you can't pass it any. For this reason, a class that inherits directly from object
can't safely use super
in general (unless it explicitly calls it with no arguments, in which case there's no point in calling it at all). It's better to create a "topmost" superclass from which ABC
and DEF
inherit.
Upvotes: 7