0

A basic question on inheritance and "self" here.

Good code with no error: (Correction: Turns out this is NOT GOOD, either. Please refer to MisterMiyagi's answers below.)

class A:
  def __init__(self, a):
    self.a = 10
    self.b = { 'a': 10, 'b': 20 }
    self.c = [ 1, 2, 3 ]

class B(A):
  def __init__(self):
    super().__init__(self)
    print(self.a)
    print(self.b)
    print(self.c)

i = B()

Bad code with errors:

class A:
  def __init__(self, a, b, c):
    self.a = 10
    self.b = { 'a': 10, 'b': 20 }
    self.c = [ 1, 2, 3 ]

class B(A):
  def __init__(self):
    super().__init__(self)
    print(self.a)
    print(self.b)
    print(self.c)

i = B()

ERROR:

Traceback (most recent call last):
  File "./o", line 16, in <module>
    i = B()
  File "./o", line 11, in __init__
    super().__init__(self)
TypeError: __init__() missing 2 required positional arguments: 'b' and 'c'

Could you please explain why it has to be:

class A:
  def __init__(self, a):

but neither

class A:
  def __init__(self):

nor

class A:
  def __init__(self, a, b, c):

?

Thank you!

5
  • 2
    Note that super().__init__(self) is not correct, unless the intention is to bind a=self. super().__init__() should be used, and A.__init__ should not take any arguments aside from self. Commented May 29, 2020 at 17:48
  • 1
    I don't understand the question. You are not using any of the arguments in the A()'s constructor, so why not either make them optional, or not pass them at all? Commented May 29, 2020 at 17:48
  • 1
    Your super call is busted - it should be super().__init__(), and A.__init__ shouldn't take an a argument. Commented May 29, 2020 at 17:48
  • Since def __init__(self, a) versus def __init__(self) is a typo/error, can you clarify why you would expect def __init__(self, a, b, c) to be correct? You are not providing b or c anywhere. Commented May 29, 2020 at 17:51
  • I think MisterMiyagi and Monica gave the direct answer. When I used super().__init__() instead, everything's charm now. Thank you! Commented May 29, 2020 at 17:56

3 Answers 3

2

A's constructor takes 3 arguments, and you're giving it 1. That's the reason for the error.

Since it doesn't use any of the 3 arguments, it should take 0 arguments, and B in turn should give it 0 arguments:

class A:
  def __init__(self):
    self.a = 10
    self.b = { 'a': 10, 'b': 20 }
    self.c = [ 1, 2, 3 ]

class B(A):
  def __init__(self):
    super().__init__()
    print(self.a)
    print(self.b)
    print(self.c)

i = B()
Sign up to request clarification or add additional context in comments.

Comments

1

Calling a method on super() already passes on the first argument (self or cls) properly. Do not pass along self explicitly.

class A:
  def __init__(self):  # receive implicit self
    self.a = 10
    self.b = { 'a': 10, 'b': 20 }
    self.c = [ 1, 2, 3 ]

class B(A):
  def __init__(self):
    super().__init__()  # pass self implicitly
    print(self.a)
    print(self.b)
    print(self.c)

i = B()

A call such as super().__init__(self) passes self to the method twice. Thus, the method must take two parameters, e.g. def __init__(self, a). Use just super().__init__() and def __init__(self) to pass along self once instead.

Comments

1

When you call a method on an object, the object will be assigned to the self argument of the function, for example:

class A:
  def someFunc(self):
    pass

a = A()
a.someFunc() # self == a

When you are calling the super function, your own class will be assigned to self, you should be calling super like this:

class A:
  def __init__(self, a):
    sef.a = a

class B(A):
  def __init__(self):
    a = # something
    super(self).__init__(a)
    print(self.a)

i = B()

The problem with your code is that you are assigning a to self, and then not providing a value for b and c.

Comments

Your Answer

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.