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 trialNoor Elharty
2,630 Pointsclarify somethings about MRO of this video
so here in this video when we create an instance of Thief Class.
The first __init__
to be called is "Sneaky" __init__
because
on the top of MRO for the Thief Class is Thief Class itself , and since Thief class doesn't have __init__
method The MRO falls back to the next class in it's chain of commands which is "Sneaky" class which have __init__
method so "Sneaky" __init__
called First and our instance acquires (sneaky attribute) , then Agile __init__
called second and our instance acquires (agile attribute) ,
then Character __init__
called last and our instance acquires (name attribute)
what super() function do here is that it gets *args and *kwargs code all the way down from Character class (Sneaky supers Agile and Agile supers Character)
did i get it right ? i know i said much , but i hope someone can validate how i get it
[MOD: added ` around __init__
to preserve characters]
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYes. You have it correct! Execution would stop at the first __init__
method without a super()
call.
Derrick Thomin
10,300 PointsSince the contents of kwargs at different points is what was tripping me up, I did a similar thing as ericfromportland did in the above post (thanks for the good idea!). Posting here in case it helps anyone this all happens right after this line of code is called in play.py: derrick = Thief(name="derrick", sneaky=False)
----------
Right Before super call in Agile
agile: True
Args: ()
Kwargs: {'name': 'derrick', 'sneaky': False}
----------
----------
Right before super call in Sneaky
sneaky: False
Args: ()
Kwargs: {'name': 'derrick'}
----------
----------
Right After __init__ super call in Character
Name: derrick
Kwargs: {}
----------
character | name==derrick
----------
Right After super call in Sneaky
sneaky: False
Args: ()
Kwargs: {'name': 'derrick'}
----------
----------
Right After super call in Agile
agile: True
Args: ()
Kwargs: {'name': 'derrick', 'sneaky': False}
----------
Chris Freeman
Treehouse Moderator 68,441 PointsWell done Derrick Thomin! That’s also what I did to really understand it.
Another key insight is, at each method called, the receiving parameter *args and **kwargs aren’t the same as the calling *args and **kwargs arguments. Instead, the receiving ones are recreated from the left-overs after the positional and keyword parameters in the called method have been assigned.
Jay Reyes
Python Web Development Techdegree Student 15,937 PointsJay Reyes
Python Web Development Techdegree Student 15,937 PointsA simple explanation, thank you both. I'm afraid the instructor failed to mention that calling multiple classes in the creation of an instance creates a hierarchy of parenthood.
So if it was
We would have:
I guess Thief is the child of Agile?
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsI would say they are more like “ordered co-parents” rather a hierarchy of parents. The parents classes
Agile
andSneaky
act more like “add-ins” that modify the parent classCharacter
. The add-ins can be listed with either one first. Add-ins are not typically intended to be a sole parent class.ericfromportland
Courses Plus Student 3,400 Pointsericfromportland
Courses Plus Student 3,400 PointsI had a really tough time with this conceptually, and it took me a little while to get my arms wrapped around the content. I embedded a bunch of print statements into my code to help me follow the program flow.
Using the same order of super classes as Kenneth's code example (Agile, Sneaky, Character), I created an instance of Thief and watched the control cascade from Agile to Sneaky to Character, and then back again. It helped me track what was going on, and helped me to further understand the great information provided by Chris and others. I thought I would post my output just in case it might help someone else. Cheers!
With the exception of the first two lines, this is all output from print() statements that I added inside my Agile, Sneaky, and Character classes:
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsNicely done ericfromportland!