Python

Nicholas Rogers
Nicholas Rogers
6,411 Points

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 47,995 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!!