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!

Ernestas Petruoka
Ernestas Petruoka
1,856 Points

Help I am stuck!! How do I override add_item method?????

I am stuck a little so please someone explain what do I need to do in this challange??? And please explain super() function a little. I do understand that it takes piece of parent class and make a copy of that which you can use, but how do you do that is still mystery for me :(

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):
Adam Tyler
Adam Tyler
14,865 Points

I'm stuck on this too have you found an answer?

2 Answers

Ernestas Petruoka
Ernestas Petruoka
1,856 Points

Yes! You need to call your Inventory class from SortedInventory class by creating new method in SortedInventory class and in that method using super() function. And then for 3th task you need sort list with list.sort(). My whole code looks like this:

class Inventory:
    def __init__(self):
        self.slots = []

    def add_item(self, item):
        self.slots.append(item)
class SortedInventory(Inventory):
    def add_item(self):
        super().add_item(item)
        list.sort(self.slots)

Hope that will help you !

Pitrov Secondary
Pitrov Secondary
5,121 Points

I copied the code over to my challenge, but it doesn't work. Did that work to you?

Pitrov Secondary
Pitrov Secondary
5,121 Points

I figured it out. You have to do

def add_item(self, item):

and not

def add_item(self):
Alan Stride
Alan Stride
1,660 Points

I cannot move on from this task. I have tried everything I can think of, and even copied and pasted the code recommended in previous answer, but it still won't get accepted as correct. As usual (don't understand why!), we get no feedback as to why the code is wrong, not even which line is causing the problem, so at this point I am just guessing any combination of words. Any help appreciated.

Task 2/3

class Inventory:
    def __init__(self):
        self.slots = []

    def add_item(self, item):
        self.slots.append(item)
class SortedInventory(Inventory):
    def add_item(self):
        super().add_item(item)