Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Understanding the benefits of super() overriding __init__
What functionality are we missing within the subclass that we get using the super() override of
Chris FreemanTreehouse Moderator 67,622 Points
Good question. But there might be some misunderstanding in how you phrased it. The function
super() does not "override" the
__init__(). Rather, it executes the
__init__() of the parent class within the current subclass execution of
super() call is not included in the
subclass.__init__() method, the
subclass.__init__() completely overrides the
parent.__init__() method preventing the parent's
__init__() from executing. This can be a legitimate useage provide, there is nothing needed from the parent's initialization code.
It some cases the subclass does not need to modify the actions of the
parent.__init__() at all. In this case, the subclass does not include an
__init__() method. This will cause the parent's
__init__() method to be called automatically.
Once the method
__init__() is added to the subclass, the question becomes "Is there anything needed in the parent's
__init__() method?" If yes, the a
super() call is needed. The next question becomes "Does the subclass need to initial items before or after (or both) the parent's
__init__() method runs?" If subclass needs to initialize items before the parent,
super() is called last. If subclass needs to initialize items after the parent,
super() is called first. If both,
super() is called in the middle of the
subclass.__init__() method code.
If you need more help, please post back. Good luck!
This is very good explaining