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!
super().__init__(self)is not correct, unless the intention is to binda=self.super().__init__()should be used, andA.__init__should not take any arguments aside fromself.supercall is busted - it should besuper().__init__(), andA.__init__shouldn't take anaargument.def __init__(self, a)versusdef __init__(self)is a typo/error, can you clarify why you would expectdef __init__(self, a, b, c)to be correct? You are not providingborcanywhere.