Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Inheritance Multiple Superclasses

Safae Merigh
Safae Merigh
9,827 Points

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?

1 Answer

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.