Python Object-Oriented Python Inheritance Super!

joshua korir
joshua korir
1,445 Points

what is wrong with my code

inheritance

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().__init__()

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 58,897 Points

You are close. You’ve overridden the __init__ method not the add_item method.

Edit: You can override add_item the same way __init__ was over ridden. Just change the names and be sure to include the item parameter. The first task you’re creating a redundant method to the parent’s method. It’s a needed set up for Task 2.

Edit 2:

  • literally replace __init__ with add_item, in your original code.
  • add item parameter to both the method definition and as an argument to the super call: super().add_item(item)

Post back if you need more help. Good luck!!!

joshua korir
joshua korir
1,445 Points

how is that done? i thought the subclass automatically enables us to access all method s of it parents

joshua korir
joshua korir
1,445 Points

i dont think the video covered this

Chris Freeman
Chris Freeman
Treehouse Moderator 58,897 Points

It didn't cover this exact scenario. It requires you to apply the concepts to a slight newer tasks. All of the concepts have been shown, just not this exact task.