Python Object-Oriented Python Inheritance Super!

Jeffrey Robinson
Jeffrey Robinson
2,481 Points

add_item

I've tried a number of solutions to this problem but there is so little feedback I'm not even sure if I'm even reading it right.

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

    def add_item(self, item):
        self.slots.append(item)

class SortedInventory(Inventory):

    def __init__(self, item):
        super().add_item(item)
Dave StSomeWhere
Dave StSomeWhere
19,768 Points

Are you stuck on task 2?

Double check the requirement - it looks like you are attempting to override the init() method - is that the ask?

Jeffrey Robinson
Jeffrey Robinson
2,481 Points

Good evening Dave - thank you for taking the time to answer my question. The question is:

"Now override the add_item method. Use super() in it to make sure the item still gets added to the list."

I feel like a dummy, but I don't even know what that means. Override the add_item method?

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,768 Points

Hey hey Jeffery,

The concept of inheritance is very important and you should probably do some follow up searches on it and go through the video again until it makes sense. No use to moving on until you got this :smile:.

In task 1 you created a sub class of Inventory

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

    def add_item(self, item):
        self.slots.append(item)

# specify a class in the parenthesis and you have a subclass
class SortedInventory(Inventory):
    pass

The SortedInventory class at this point is the exact same as Inventory - because - it inherited everything from Inventory, which is two methods one called __init__() and one called add_item().

Now we have the ability to add additional properties (variables) or methods (functions) to our subclass. We also have the ability to say. Hey I want to do something different with one of the two methods inherited from Inventory - this is called overriding the method. All we need to do to override the method is to define in our subclass. Like you did with __init()__ and should of done with add_item(). When you override a method your subclass method is executed instead of the base class method.

Once you override the method, you might still want to execute the base class method in addition to executing your subclass features. This is where super() comes into play. Super calls the base class method. Hope that makes sense - just let me know

What it should look like is:

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

    def add_item(self, item):
        self.slots.append(item)

class SortedInventory(Inventory): 

    def add_item(self, item):  # override add_item
        super().add_item(item)  # call the Inventory add_item() method
        #  put SortedInventory specific code here

Does that make any sense?

Jeffrey Robinson
Jeffrey Robinson
2,481 Points

Good morning Dave,

Thank you very much. Yes, that totally makes sense. I was not picking up what it meant to "override" from the video. Thank you for your help.

Jeff