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 trialNicolae Laurentiu
1,538 PointsI don't understand why do I have to use super() function. It is so confusing. Could anyone explain me in a more basic ?
No idea how to resolve the exercise. Please, I need help with the super() class. I don't understand it at all .
class Inventory:
def __init__(self):
self.slots = []
def add_item(self, item):
self.slots.append(item)
class SortedInventory(Inventory):
def __init__(self, item):
self.slots = []
super().__init__()
def add_item(item):
super().add_item(item)
2 Answers
Steven Parker
231,275 PointsYou might think of "super()" as meaning "use the one in the base class instead of my own".
For example, both classes shown here have a method called "add_item", but if you are writing code inside the "SortedInventory" class you don't want to call it's own method. By putting "super()" in front of "add_item()" you are calling the one in the "Inventory" class instead (the base or "parent" class).
You have actually done that part correctly in your challenge code, but there are two other issues preventing it from passing:
- the instructions did not ask you to override the "
__init__
" method (you can remove that code) - the "add_item" method needs "self" as the first parameter
Nicolae Laurentiu
1,538 PointsThank you Chris. You are right.
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsExtending Steven Parkerโs answer, when a class method is created with the same name as a method in one of its parent classes, this new method overrides the parent method thus blocking access to the parent method.
The use of
super().__
method_name__
allows you to bypass the name override by saying โskip the local name space and search the parentโs namespace for this method.โThe main use of
super()
is to extend the parentโs method of the same name. Note that thesuper()
statement may be used at the beginning, end, or middle of a method so you can run the local methodโs code after, before, or both, respectively, the parentโs method code.Post back if you need more help. Good luck!!!
Nicolae Laurentiu
1,538 PointsNicolae Laurentiu
1,538 PointsI still get an error after modifying the code: What is the problem? I have tried the code in the python IDE and it worked.
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsYou seem to be mixing Tabs and Spaces