Python Object-Oriented Python Inheritance Super!

Ernestas Petruoka
Ernestas Petruoka
1,855 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
5,095 Points

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

1 Answer

Ernestas Petruoka
Ernestas Petruoka
1,855 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,119 Points

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

Pitrov Secondary
Pitrov Secondary
5,119 Points

I figured it out. You have to do

def add_item(self, item):

and not

def add_item(self):