Python Object-Oriented Python Inheritance Multiple Superclasses

MoatazBellah Ghobashy
MoatazBellah Ghobashy
9,252 Points

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
182,578 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
182,578 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.

Kafe Hezam
Kafe Hezam
10,990 Points

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
182,578 Points

I added a comment with more details onto my answer.

Youssef Moustahib
Youssef Moustahib
7,776 Points

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

Kafe Hezam
Kafe Hezam
10,990 Points

Understood, thanks!!!!:)