If a class that inherits from another class automatically also inherits its constructor then why bother using super().__init__? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

If a class that inherits from another class automatically also inherits its constructor then why bother using super().__init__?

Whats the purpose? Example: class A: def __init__(self, y, x): self.y = y self.x = x class B(A): def __init__(self,x): super().__init__(x, x) Why not just: class B(A): pass

28th Apr 2022, 8:07 AM
Lenoname
1 Answer
+ 3
For one, you pass values in the super constructor. So it'll need those values regardless now. Plus if you dont do the super's init method, you won't get the x and y attributes cause they will never be set. It doesn't just automatically happen, you still have to do it.
28th Apr 2022, 9:05 AM
Slick
Slick - avatar