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 Multiple Superclasses

Why do we call super() in attributes.py and

The classes Sneaky and Argile in each one of them, we call super().init(*args, **kwargs)

From what I understood, this line means that we are calling the init method of the parent class. But since each of them is defined this : class Sneaky: ...

class Agile:
    ...

It means that they do not have a parent class. Right? So why are we having these lines?

2 Answers

First, thank you for posting this question. It REALLY made me think about the super() class in a way I hadn't previously.

Let's use some sample code to try to make sense of this:

class Thief(Agile, Sneaky, Character):
    sneaky = True

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

    def pickpocket(self):
        return self.sneaky and bool(random.randit(0, 1))

    def hide(self, light_level)

You can see that we passed in several classes into Thief, including Agile and Sneaky as well as Character. From the research I'd just done, the super() function inherits from the class following the current class in the list of arguments. That means that using class Thief(Agile, Sneaky, Character) results in Thief inheriting from Agile, then Agile inheriting from Sneaky and Sneaky inheriting from Character. What really helped me was looking up Method Resolution Order.

Another thing I read is that every class inherits from the object class, so if there's no parent class stated explicitly I expect that the super() call would be referring to the object class. That I'm a little less confident in.

Craig Stephenson
Craig Stephenson
2,490 Points

Once again, the problem (for me) with your example is that it does not explain Kenneth's code. I have replayed the video several times, and in Kenneth's code the Thief class does not have an init method.

For me what is going on is that the Thief class is defining the order of inheritance, which is then used by the calls to super() in the Agile and Sneaky classes.