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 Advanced Objects Subclassing Built-ins

Arcee Palabrica
Arcee Palabrica
8,100 Points

What's happening when we put super().__init__()?

Hey guys so in the video around 5:04 we put super().__init__() ... anyone who can help me understand the reason why it's ignoring anything that was passed in?

How does it work?

And does it only work when we're subclassing built-ins? I tried testing it on the following to help me see how it really works but I don't think it's applicable in this setting...

class Animal:
    def __init__(self, name, *args, **kwargs):
        self.name = name

        for key, value in kwargs.items():
            setattr(self, key, value)



class Dog(Animal):
    def __init__(self, name, sound, *args, **kwargs):
        super().__init__() # <--- Tried it here 
        self.name = name
        self.sound = sound

Getting a TypeError: init() missing 1 required positional argument: 'name'

that's why it made me think it's only applicable in subclassing built-ins...

2 Answers

Steven Parker
Steven Parker
230,995 Points

You almost had it. But the Animal class requires at least one argument so you need to pass that along:

        super().__init__(name)

And you won't need that "self.name = name" anymore since the superclass does that for you. Plus, you may as well pass along any additional arguments and let the superclass handle them for you also:

        super().__init__(name, *args, **kwargs)
Arcee Palabrica
Arcee Palabrica
8,100 Points

Steven Parker Hey man... thanks. :) But we also used super().__init__() without any parameters in the video Subclassing Built-ins I think around 5:04. How does it work and how is it ignoring anything that was passed in? can we also apply that in the Animal class I made? thanks so much

Steven Parker
Steven Parker
230,995 Points

The FilledList class in the video is a subclass of list, and list doesn't require any arguments to instantiate an new object. But your Animal class is defined so that it requires at least one "name" argument to work, so it must be passed to super().__init__(name) when it is called from Dog. That's what that error message was trying to tell you.

Arcee Palabrica
Arcee Palabrica
8,100 Points

Alright now I understand thanks Steven Parker.. :)