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

Python Object-Oriented Python Inheritance Multiple Superclasses

Son-Hai Nguyen
Son-Hai Nguyen
2,481 Points

Er.. why Kenneth changed the Character class to the last one called?

When class Thief(Character, Sneaky, Agile): still worked just fine, why Kenneth changed the Character class to the last one called in class Thief(Sneaky, Agile, Character):? I understand he's trying to demo us a case where error may occur, but in reality is there any case we will need to do that? What's the point behind doing that?

Thanks guys!!!

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Good Question. The specific difference between the parent class Character and the other Agile and Sneaky classes is that the Character.__init__ does not have a call to super() while the other two do have a super() call. With Character as the first parent class, the Agile.__init__ and Sneaky.__init__ will never be executed. By moving Character to the last position, the super() in Agile.__init__ can call Sneaky.__init__ which calls Character.__init__.

In the example, code it still works with Character as the first parent because it runs a loop to add all keyword arguments as attributes, such that self.agile and self.sneaky will get set regardless if the other __init__ methods are run. Where this would break, is if the other __init__ methods contained additional code beyond simple attribute assignment such as other attribute initialization that isn't defined by a keyword argument.

Post back if you need more help. Good luck!!

Son-Hai Nguyen
Son-Hai Nguyen
2,481 Points

Hi Chris,

Thank you so much for your reply. I see it much clearer now. But there's one thing I'm still left wondering, that is it true that the first class called is the parent class? Is it always setup this way?

Thank you again, Chris!!!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Character, Thief, andSneaky would all considered parent classes. The use of β€œparent” and β€œchild” comes from other languages. In Python, when describing inhertance and multiple inheritance, there are more precise terms. The classes listed within the parentheses of the class declaration are called base classes and the class being defined is call the derived class

class DerivedClassName(BaseClassName):
    <statement-1>
    .
    .
    .
    <statement-N>

class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    .
    .
    .
    <statement-N>

Post back if you need more help. Good luck!!!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I should add the terminology class and subclass is also used. A given DerivedClass is also a subclass of each BaseClass listed in the signature line.

Son-Hai Nguyen
Son-Hai Nguyen
2,481 Points

Thank you so much Chris!!!

Myles Brathwaite
Myles Brathwaite
1,971 Points

Thank you Son for asking these questions honestly so helpful

Son-Hai Nguyen
Son-Hai Nguyen
2,481 Points

Hi Chris. Thank you so much for your explanation. So basically Python will take all Sneaky, Agile and Character as parents of Thief class? Are they considered equally?

One more thing, so what does the super() do in class Agile and Sneaky? As I understand, the super() will inherit from the parent, but what are parents of Agile and Sneaky in this case, when class Sneaky: and class Agile: when there's no parentheses comes with them?

Thank you so much for your support Chris!!!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Given multiple base classes, they are considered in the order presented. Each object reference is first search for within Thief instance, then the 'Thief` class, then in each of the base classes until a match is found. This is called the Method Resolution Order (MRO). The MRO can be seen:

  • of an class: classname.__mro__
  • of an instance instance.__class__.__mro__
Son-Hai Nguyen
Son-Hai Nguyen
2,481 Points

Thank you so much Chris. And can you please tell me what does the super() function do in class Sneaky: and class Agile:?

Thank you again Chris!!!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

super() calls the method of the same name that is in the next base class along the MRO.