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
Aaron Banerjee
6,876 Pointsmultiple inheritance confusion
hi everyone, im trying to understand multiple class inheritance but i am struggling. Can someone please explain how the thief class works below when inheriting from the agile sneaky and character classes and how it automatically knows to take in a name and sneaky and agile as attributes.
from thieves import Thief
kenneth = Thief("Kenneth",sneaky=false)
print(kenneth.sneaky)
print(kenneth.agile)
print(kenneth.hide(8))
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsGreat question! When the Thief class is instantiated, the __init__ methods are run in the following order stopping at the first found.
- the local Thief definition
- each inherited class in the order listed
- Agile
- Sneaky
- Character
If a
super()is reached, then next__init__is looked for down the list above.
Since both Agile and Sneaky have a super() call, the __init__ in all three inherited classes are run. These assign the attributes sneaky and agile.
Post back if you have more questions. Good luck!!!
Aaron Banerjee
6,876 Pointswhy did we make it so agile and sneaky have the super function as they dont need to inherit from any other class?
Chris Freeman
Treehouse Moderator 68,468 PointsYou are correct that Agile and Sneaky don't need super inheritance themselves. The super() is used to daisy-chain the execution of __init__ methods in all of the inherited classes. Without the use of super, the __init__ execution would stop with the first method found.