-1

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)

bignose
32.7k16 gold badges81 silver badges116 bronze badges
asked Oct 12, 2016 at 9:07
4
  • 5
    _init_ -> __init__? Commented Oct 12, 2016 at 9:11
  • 1
    When I run this code and fix the indent on super(Parent, self)._init_(a, b) I get no exception, except for the one you raise Commented Oct 12, 2016 at 9:12
  • OMG!!! I wasted hours on this, __ is really the problem!! Python should give me a better error message.. T T Commented Oct 12, 2016 at 9:15
  • Maybe you remove your question in that case? The title is very misleading. "Python child class initialiser argument" I think there is no use to save code with typos. Commented Aug 30, 2023 at 10:09

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

There is no such thing as __super__. And the problem is with the _init_ missing extra underscores which your answer does not solve

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.