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 trial

Python Object-Oriented Python Inheritance Super-Duper!

Philip Schultz
Philip Schultz
11,437 Points

Understanding Super()

Hello Everyone, I find this video to be very confusing, so I'm hoping someone can help me grasp the concept completely. If I'm understanding this correctly, whenever we make a subclass (like Thief is to Character) we have to make our init parameters the exact same as the parent class parameters followed by whatever additional attributes to the subclass if wanted, right? Why is it that sneaky doesn't have to be a parameter in the init method subclass/Thief, is it because it is a constant that we don't want changed (all thieves are sneaky)? In previous videos he declared the sneaky attribute directly in the init parameters ,like so

def __init__(self,name, sneaky = True,  **kwargs)

In this video he is making it true in the init method? Like so,

def __init__(self,name, **kwargs)
        super().__init(name, **kwargs)
        self.sneaky = Ture

Are both acceptable? Is one way better practice than the other? Also, the super function is pretty much relaying the arguments to the parent class, right? Sorry, I know this is a lot of questions.

1 Answer

Hi there!

Yep, spot on - we know all thieves will be sneaky and we don't want that to be changed, so we declare it inside the method. If you think sometimes you might want to change it, but normally it will be a certain value, then a default argument is probably the best way to go and if it's likely to be unique to the instance of the class you probably don't want a default argument or value because you want an error to get thrown if some how an instance of a class is created without supplying that argument.

And yes, in essence what super does here is go and grab the parent class for you so that you can call it's init method. The only mistake if I'm reading it correctly is that no, your child class's init doesn't need all the arguments of the parent class's one. It just needs to have values for them by the time that it calls the parent class init. The values could come either from the arguments of the child class method, or be declared within it.

So example:

class People():
    def __init__(self, name, favouriteTeam):
        self.name = name
        self.favouriteTeam = favouriteTeam

class BulldogsFan(People):
    def __init__(self, name):
        team = "Bulldogs"
        super().__init__(name, team)

Simple obviously, but as you can see the child class doesn't need all the paramters of the parent, it just needs answers for those questions when it wants to make the call.

Hope it helps.