Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialnkennedynoble
9,268 PointsUnderstanding the benefits of super() overriding __init__
What functionality are we missing within the subclass that we get using the super() override of __init__
2 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsGood 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 __init__()
.
If the 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!
omar ahmed
Courses Plus Student 216 PointsThis is very good explaining
Gali B
2,082 PointsGali B
2,082 PointsThanks for that forward answer, sir! very clear and easy to understand after I've been struggling with this quite a lot.
Jeremy Rambis
Courses Plus Student 3,288 PointsJeremy Rambis
Courses Plus Student 3,288 PointsThank you for this explanation, this helped me to grasp what super() does and why it's important/useful!
Jason Tran
7,393 PointsJason Tran
7,393 PointsThis was a very good explanation. Thank you for clearing it up!
Daniel Murray
3,294 PointsDaniel Murray
3,294 PointsThank you Chris, this detail should be in the video or Teacher's notes.
Thomas Bynum
4,616 PointsThomas Bynum
4,616 PointsThis made it all make sense for me. Thank you!
Elena Sefranek
2,056 PointsElena Sefranek
2,056 PointsHi Chris, thank you so much for this wonderful explanation. It really made it clear why you need super() and what would happen in the various cases (with super, without, order of super etc.) . I agree with Daniel, this should be in the teacher's notes for everyone to see.