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

Philip Schultz
Philip Schultz
11,437 Points

Understanding MRO.

Is the point of this video to inform us that if we are going to design our code to inherit from multiple classes then only use keyword arguments because they are considered explicit? If we do this then it will make its way down the MRO road, so to speak? I'm still very confused on how MRO works. I put a print statement before and after the super in each class and this is how I'm understanding it (Please let me know if I'm wrong).

I'm using the same order as in the video,

class Thief(Agile, Sneaky, Character)

So, since Agile is first on the list, all arguments are sent to Agile where all arguments will be cross-examined with the Agile parameters, and if there is a match the value will be assigned, then everything that doesn't have a matching keyword will be packed up in *kwargs and sent to the Sneaky class (via super), where the same thing will happen - all arguments get unpacked, cross-referenced with the Sneaky parameters (proper values assigned), then packed up in kwargs and sent to Character. Then everything within the Character inint method will run and all values will be set.


Now that everything made it to the Character class and everything in the Character init method has run, now it has to go back to the Agile and Sneaky classes and finishing running everything in their init methods(or everything under their super). So, it will first go back to the Sneaky class and finish it's init method, then go back to the Agile class and finish the rest of its init method (in that order).

Do I have it confused anywhere? Phew. I'm sorry, I know this is a lot, but I'm really stuck here and I'm trying to get a clear understanding of how MRO works.

Thank you, everyone.

1 Answer

I think you got the part about how the calls work down (based on my understanding of this stackexchange discussion: https://stackoverflow.com/questions/52013570/how-mro-super-works-in-python )!

However, MRO (the order rather than the manner in which methods are called) is more than just the order of the bases in your class definition. In the Teacher's Notes for a previous video, there was a link to someone's blog ( https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ ) that gave a brief rundown about MRO, which they boil down to two rules:

  1. Subclasses precede superclasses. This means that classes that are defined to have a parent class will precede their parent class(es). If Thief were to have its own explicit init method, that method would precede the init methods of its parents.

  2. If there is ambiguity about lineage, respect the base order. The base order for your Thief class is the order of the parents you wrote, i.e. Agile, Sneaky, and then Character. Since Agile, Sneaky, and Character are not supers or subs to each other, rule 1 does not apply, and we follow rule 2.