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 trialQuinton Dobbs
5,149 PointsI think I understand, but the video still confuses me
So in the video, it makes is seem that super() is required to change the "sneaky" attribute, or at least that's how it comes across to me. And the code is written like so:
def __init__(self,name,sneaky = True,**kwargs):
super().__init__(name, **kwargs)
self.sneaky = sneaky
But, you can completely remove the super() line and you can still change "sneaky". Like so:
def __init__(self,name,sneaky = True,**kwargs):
self.sneaky = sneaky
But, when you remove the super() you are no longer able to add new arguments (I think because the parent class is the only one with setattr() and they are no longer linked because you removed super())
So, it seems to me like super() isn't really necessary for changing the "sneaky" attribute, but it is kind of like calling a function(method) within a function(method). That is what I have been able to glean from my own experimenting, but it doesn't seem to line up with what Kenneth is trying to get across. I hope it makes sense, I'm notoriously bad at explaining things.
4 Answers
Steven Parker
231,275 PointsIt sounds like you do have a basic understanding. The call to super has nothing to do with setting sneaky. But it does allow the base class constructor to set name and any other supplied keyword arguments.
The storage of sneaky is unique to this class, and the base class knows nothing about it.
Steven Parker
231,275 PointsYou're right again, that the super passes arguments "up to" the parent.
But since "sneaky" only exists in the "Thief" class (and "Character" knows nothing about it), it would not be inherited by "Warrior".
Also, if you create a class that has no parent, you would not call super inside it.
Quinton Dobbs
5,149 PointsMan, super() is really hard to rap my head around, but I think I have it and the perfect analogy after stumbling my way through. So, when you write:
class Thief(Character):
sneaky = True
def __init__(self,name,sneaky = True,**kwargs):
super().__init__(name, **kwargs)
self.sneaky = sneaky
It is kind like Thief having his parents(Character) over for dinner. When he tells them
super().__init__(name, **kwargs)
he's telling his parents, "you bring the name and the **kwargs." and when he's saying
self.sneaky = sneaky
he's telling them, "I'll handle the sneaky."
So when you call Thief's house you know they're having some name, **kwargs, and sneaky for dinner. But, if you call the parent's house the probably have some leftover name and **kwargs but the sneaky stayed with Thief.
Steven Parker
231,275 PointsThat's a rather complicated and funny analogy but I think you pretty much nailed it. I might make one change, he tells his parents "Here, take this name and kwargs and cook them for me. I'll handle the sneaky."
Quinton Dobbs
5,149 PointsThanks Steven, you're a gentleman and a scholar.
Anthony Albertorio
22,624 PointsQuinton Dobbs, I very much enjoyed your analogy. It helped me understand super() better. Thanks Steven Parker for adding in your ideas about super() and further cementing in the concept.
Jessica Solano
6,987 PointsThis was excellent! I seriously read through all of the Q&A on this video because I couldn't understand it, even outside of Treehouse and this was the one that made the most sense and allowed me to pass my code challenge. Thanks for the humor guys! :)
Ramsley Brice
4,767 PointsHey guys, just let you know that this conversation was indeed gold.
Quinton Dobbs
5,149 PointsQuinton Dobbs
5,149 PointsAwesome, thanks for the feedback!
Quinton Dobbs
5,149 PointsQuinton Dobbs
5,149 PointsSo, i was moving along in the course and I think I might have it a little bit off. I was under the impression that super() passed something down from the parent. But, now it seems like the opposite where something is being passed up from the child.
For example, say there were a "Thief" class and a "Warrior" class and the both had a parent of "Character." If you had written in the "Thief" class:
Would the "Warrior" also be sneaky sense they share the same parent?
Also, if this is true, if you write the same method shown above into a class without a parent(written in a hidden, universal parent call "object") is it automatically applied to every class?,