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 Object-Oriented Python Inheritance Super!

hector alvarado
hector alvarado
15,796 Points

Can'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.

inventory.py
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
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

In 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!!