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 trialMoatazBellah Ghobashy
9,518 Pointsmultiplie 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
231,269 PointsWhat "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().
Kafe Hezam
11,070 PointsHi 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
231,269 PointsI added a comment with more details onto my answer.
Youssef Moustahib
7,779 PointsI tried the print method, it only printed from character, not agile or sneaky
Kafe Hezam
11,070 PointsUnderstood, thanks!!!!:)
Steven Parker
231,269 PointsSteven Parker
231,269 PointsA 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.MoatazBellah Ghobashy
9,518 PointsMoatazBellah Ghobashy
9,518 PointsThanks a lot :)