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

multiplie superclasses....super() in sneaky and Agile

why call super() in Sneaky and Agile!!!, although these classes are not children for any class... Please help

3 Answers

Steven Parker
Steven Parker
221,293 Points

What "super()" does depends on the MRO, and in cases of multiple inheritance it may call a sibling class instead of a parent. That's what you see in these examples.

For more details, see the Python documentation for super().

Steven Parker
Steven Parker
221,293 Points

A little experimentation might be more revealing than documentation or explanation. You might try printing a message at the end of the __init__ method of each class. But here's what you'll see if you do that and instantiate an object of the class that has the multiple inherittance of (Sneaky, Agile, Character). "Sneaky" calls super, and the MRO directs it to Agile, which also calls super, which directs it to Character. So the printed messages will show that the inits for Character, then Agile, then Sneaky all ran, and completed in that order.

Thanks a lot :)

Hi Steven,

I really still didn't understand why call super() in Sneaky and Agile :( MRO means: Method resolution order:

  • Childclass
  • Parentclass
  • builtins.object So, if python doesn't find it in the childclass then it goes to the parent class. If Python does not find it in the Child/Parent class then python should find it in the builtins.object class.

I read the python doc but still didn't understand:( Could you please provide examples so that I can understand it?:)

Steven Parker
Steven Parker
221,293 Points

I added a comment with more details onto my answer.

Youssef Moustahib
Youssef Moustahib
7,779 Points

I tried the print method, it only printed from character, not agile or sneaky

Understood, thanks!!!!:)