inheritance of constructors | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

inheritance of constructors

class A(object): def __init__(self): print (1) class B(A): def __init__(self): super(A, self).__init__() print (2) B() Why print just 2?

4th Nov 2018, 9:32 PM
Александр Лебедев
Александр Лебедев - avatar
4 Answers
+ 3
Hmmm, maybe because A has no Parent?
5th Nov 2018, 8:23 AM
Sebastian Keßler
Sebastian Keßler - avatar
+ 2
The super-Function didn't need any Arguments. Just change the line: super().__init__()
4th Nov 2018, 10:22 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 1
Yes, exectly! You are right!
5th Nov 2018, 4:26 PM
Александр Лебедев
Александр Лебедев - avatar
0
It's properly. Yes, but its optional in python3 and if I write super(A, self), there is no error. And I tried in python2.7 to lainch the code (the same code, but without the parentheses after print) and I have experimented and I have the same results: just 2 i know: if i write super(B, self) instead super(A, self), i'll get all right: 12. But I do not know, what is the different beetwen A and B in super-function? why super(A,self) do not call the __init__ of the parent?
5th Nov 2018, 3:32 AM
Александр Лебедев
Александр Лебедев - avatar