+ 2

Python inheritance

class A: def __init__(self, name): self.name = name class B (A): def __init__(self): From here do I use super()? And how would one properly instanciate

9th Sep 2019, 3:30 AM
Chad Williams
Chad Williams - avatar
2 ответов
+ 1
Each subclass inherits all methods from a superclass automatically. If you write a method with the same name in a subclass, you override the method just in the subclass. If you want to add some instructions to a method of a subclass but you want to save the behavior of the superclass's method, you must use the .super(). .super() copies all the codes in a method virtually. In your example, you shouldn't write __init__ again unless you want to override it.
13th Sep 2020, 8:11 PM
Ali