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 trialhector alvarado
15,796 PointsCan't understand what I have to do here.
Okey how I understood was that I could access the data from the parent with the super() method and a dot, so if I need a method it would be like. super.method(params).. I guess it's like a prototype for methods in JavaScript. But not sure. please help??
The instruction:
Great! Now override the add_item method. Use super() in it to make sure the item still gets added to the list.
class Inventory:
def __init__(self):
self.slots = []
def add_item(self, item):
self.slots.append(item)
class SortedInventory(Inventory):
def __init__(self):
super().item
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsIn your answer to Task 1, you do not have to override the __init__
method. If it is not defined in SortedInventory
, the __init__
method in Inventory
will automatically be found and executed. For Task 1, the body of SortedInventory
could simply be pass
For Task 2, to override a parents method, create a local method with the same name and signature: def add_item(self, item):
Then use super()
to call the parents version of the method using super().add_item(item)
I'll leave you to the work out Task 3. Hint: you only need to add one additional line to the add_item
method in SortedInventory
to modify self.slots
to be sorted.
Post back if you need more help. Good luck!!
hector alvarado
15,796 Pointshector alvarado
15,796 PointsThank you!! a LOT!