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

How does the 'name' attribute get passed to the Character class?

In the Object Oriented Python Course, video Multiple Superclasses: When the MRO is Character -> Agile -> Sneaky, how does creating an instance of Thief automatically create a character and agile instance without an __init__ method in the Thief class that calls any superclass? Without defining Sneaky as a kwarg when declaring my Thief variable, my variable also does not have access to the sneaky attribute through the MRO. Does creating an instance of a class automatically create instances of each class in the MRO? If so why doesn't my Thief variable have a sneaky attribute set in the Sneaky class?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Great questions!

In the Object Oriented Python Course, video Multiple Superclasses: When the MRO is Character -> Agile -> Sneaky, how does creating an instance of Thief automatically create a character and agile instance without an __init__ method in the Thief class that calls any superclass?

When a class is instantiated, the __init__ method is run. If it is not present, then each of the inherited classes are searched in turn until an __init__ method is found. In the MRO Character -> Agile -> Sneaky, Character has the first __init__ method. The Agile and Sneaky __init__ methods will not be executed since Character.__init__ does not call super().

Without defining Sneaky as a kwarg when declaring my Thief variable, my variable also does not have access to the sneaky attribute through the MRO. Does creating an instance of a class automatically create instances of each class in the MRO? If so why doesn't my Thief variable have a sneaky attribute set in the Sneaky class?

When the Sneaky and Agile classes are parsed during the inheritance, the lines:

    # in class Sneaky:
    sneaky = True
    # in class Agile:
    agile = True

are interpreted as if they were in the body of the Thief class code.

Post back if you have more questions. Good luck!!