I have a parent class whose initialiser has three argument, now I want to have a child class whose initialiser only has two argument, but it's telling me it has to be given three arguments when I try to create the child object.
class Parent(Exception):
def _init_(self, a, b):
...
super(Parent, self)._init_(a, b)
class Child(Parent):
def _init_(self, b):
super(Child, self)._init_(123, b)
# somewhere in the code I have:
raise Child("BAD_INPUT")
What I'm trying to do is instantiate a Child object with only one argument then in the initialiser of that Child object call Parent's initialiser and pass in two argument, one is hard-coded (123) .
Error I got:
TypeError: __init__() takes exactly 3 arguments (2 given)
1 Answer 1
you should be able to use:
class Parent(Exception):
def __init__(self, a, b):
self.a = a
self.b = b
class Child(Parent):
a = 123
def __init__(self, b):
super().__init__(self.a, b)
answered Oct 12, 2016 at 9:14
Jack Cooper
4181 gold badge4 silver badges10 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Moses Koledoye
There is no such thing as
__super__. And the problem is with the _init_ missing extra underscores which your answer does not solvelang-py
_init_ -> __init__?super(Parent, self)._init_(a, b)I get no exception, except for the one you raise