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 trialAkshaan Mazumdar
3,787 PointsWhy do we call super in the Hand class >(class Hand (list):) when it does not actually extend from any other superclass?
The first class we make we see Hand actually act as a list .But why have we called super().__init__
in it?
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsGood question! When sub-classing, or extending, another class, all of the methods of the parent class are automatically available, including the __init__
method.
If you create a method in the sub-class with the same name as a method in the parent class, the local sub-class version will override the parent version. The parent version will not be executed unless explicitly called. The super()
function is used to explicitly call the method from the parent class.
In this case, super
allows the __init__
from the parent list
to also run to initiate.
In classes of multiple inheritance, each parent also typically includes a super()
call so the next parent in the chain can run itβs __init__
. If you want to subclass Hand
further, you want the super
call so it can also be used in multiple inheritance.
Post back if you need more help. Good luck!!!
Jay Reyes
Python Web Development Techdegree Student 15,937 PointsChris Freeman - When we super().__init__
the parent class: are we only in the scope of that class's __init__
method (therefore excluding methods outside of the __init__
but still inside the class)? Or are we in the scope of the entire class - so we also "import" all the methods and attributes there?
Chris Freeman
Treehouse Moderator 68,457 Points@Jay Reyes, I don't know if I would use the word "scope". Using super()
says "search the parents namespace for a method". Extending to super().__init__()
it is specifically referencing the __init__
in the parent class. The methods of the parent class are automatically present in the current class if they have not been overridden locally. In the case of __init__
it has been overridden, so super()
is needed to access the parent's version of the method.
Akshaan Mazumdar
3,787 PointsAkshaan Mazumdar
3,787 PointsGot it ! So to sum it up the super in Hand is meant to override or call the init in //list//.
Also if we subclass (int) will we use super ?
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsIn general, you will need to use
super()
when subclassing.For immutable types such as
int
andstr
you cannot modify them once they have been created, so youβd have to usesuper().__new__()
instead ofsuper()__init__()