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 trialAkshaan Mazumdar
3,787 PointsThis Video is Confusing!!!!! Why are we using super if we are defining __init__ in the subclass as well?
Why is super required in this particular case? What does it actually simplify ?
3 Answers
cb123
Courses Plus Student 9,858 Pointseven after going through this SuperDuper video several times and doing the project work, i never got the solid foundation on the "why" I would need the super(). If you are still as confused as i was try this video by Corey Schafer, https://www.youtube.com/watch?v=RSl87lqOXDE, that covers the same topic with a different set of examples that really helped. Seeing it a different way really helped solidify for me the function of super() and how it allows "inheritance" from parent class to child and yet allowing child to build more complexity as needed.
Dave StSomeWhere
19,870 PointsI think of it as you are overriding the parent class __init__
in the subclass. Then that parent __init__
isn't executed.
Since you don't want to recode stuff already in the parent __init__
, you use super to get the parent stuff and your subclass specific stuff in your __init__
(so kinda sorta like doing an include).
Akshaan Mazumdar
3,787 PointsThank You for Your Reply!
Tyler Wells
Python Development Techdegree Student 2,678 PointsHi Dave, so thats what Kenneth means when he says its good for overwriting stuff that was already assigned in the sub class? So its basically just allowing us to be more specific within our subclasses and not take everything as given from the superclass?
Dave StSomeWhere
19,870 PointsHi Tyler,
Yes, I believe the concept that he is attempting to get across is that the base class is very general and that you get more specific with sub classes.
Not sure what you mean by "not take everything as given from the superclass" - probably more just wording than anything else - but - I think the spirit is that for most of the time the base class is generic enough that everything there can be taken as given and you are only adding items and not really look to change/replace items. (if that makes any sense)... So, yes you can change the base class, but I would generally say that if it is needed then it is a design flaw and the base class wasn't generic enough - So, yes there are situations where changes to the base class are necessary and correct - but should be the exception and not the rule.
That's why I think he used characters as a teaching example.
The base class of character is very generic. Then, the Thief class extends (extend/enhance) character by adding the Sneaky attribute (which only makes sense for a Theif). He also mentioned a Warrior class where he adds a weapon property. In general, just getting more specific and extending the base class.
For a final paragraph in this ramble... Since we are adding properties to the subclass, we then want to update the constructor method (__init()__
) to create these new properties at instantiation time. We also want to make sure we have the base class properties also properly instantiated (for the example it is just name - and any additional keyword arguments).
Tyler Wells
Python Development Techdegree Student 2,678 PointsThanks Dave!
Andrew Bickham
1,461 Pointsthank you from the future :) all these notes really helped
Corin Faife
9,564 PointsCorin Faife
9,564 Pointsthanks for sharing, you're right, the YouTube video is useful.
Dan Olson
4,875 PointsDan Olson
4,875 PointsThanks for the link. I found that YouTube video incredibly helpful as well.