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

Why do we need super().__init__() for the Hand class?

I find myself frequently tripped up on when to use super().

I thought super was used in a child class to inherit the attributes from a parent class, however. Here, Kenneth has the hand class and it accepts (list). is "list" a built-in class if we are calling super in the Hand class?

1 Answer

There seems to be two different questions in your post so I'll address them separately.

I find myself frequently tripped up on when to use super().

super() is actually a lot simpler than it might seem at first. It is not responsible for populating the child class with the attributes of the parent, that is something that happens automatically as that is pretty much a fundamental part of being a child class.

The main purpose of it is to enable you to call a method from the parent when you have provided an override to that method within the child class. As a simple example let's say the parent class has a method called do_work and then you end up defining a new do_work method inside of the child class (which overrides the parent one). If you for some reason wanted to call the method that was defined in the parent class you could not just type do_work() as that would tell Python to call the method defined within the child class. To clarify that you want the method defined in the parent class you would use super(). So the call would become super().do_work(), that would tell Python explicitly that you wanted to call the parent's do_work method, and not the one defined in the child class.

An important thing to note though is that when you define an __init__ method for a child class you always have to call the __init__ of the parent using super(). This is not due to super() doing something magical relating to initialization, but because Python requires that when you instantiate a child class the parent class has to be instantiated as well. If you don't define an __init__ method then that is not a problem since Python will just use the parent's __init__ automatically, but when you define your own, then the parent won't be instantiated unless you call the parent's __init__ method manually.

Is "list" a built-in class if we are calling super in the Hand class?

Yes, it is indeed. As is str, int, dict and pretty much all other basic data types you have been introduced to throughout the courses. When you type something like this into Python list("Hello") in order to convert a string into a list you are actually calling the list class' constructor method. And when you type a literal list like this ["Item 1", "Item 2"] Python automatically instantiates and returns a list object containing those items.

So by inheriting list you inherit all of the attributes of the real list type built into Python.

Thank you for that detailed response!!! That helps clear up a lot of my confusion.