Python is a | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

Python is a

Why does the following print only "2"? Doesn't it call A's __init__ method? I expected it to print "1" and then "2" since I thought A's __init__ would be called. class A: def __init__(self): print(1) class B(A): def __init__(self): super(A, self).__init__() print(2) obj = B()

19th Jun 2021, 2:45 AM
Brain HM
Brain HM - avatar
3 ответов
+ 3
Dublicating question..? anyways, .. for more details.. see answers.. https://www.sololearn.com/discuss/2814302/?ref=app
19th Jun 2021, 10:52 AM
Jayakrishna 🇮🇳
+ 2
This is interesting. super(B, self) is kind of same as parameterless super() and gives the expected output. class A: def __init__(self): print(1) class B(A): def __init__(self): super().__init__() print(2) obj = B() But what happens if we use super(A, self) inside class B, like in your example? can we do that ?
19th Jun 2021, 3:28 AM
Aravind Shetty
Aravind Shetty - avatar
+ 1
this is a great day of the Answering Reality.
19th Jun 2021, 3:48 PM
Shubham Bhatia
Shubham Bhatia - avatar