Python Object-Oriented Python Inheritance Super!

George Clement
George Clement
2,816 Points

confused with the statement "Use the list.sort() method " is that just creating a sort method in SortedInventory?

I am confused by the statment

Use the list.sort() method to make sure the slots list gets sorted after an item is added. Only do this in the SortedInventory class.

I don't know they want a method named sorted on the subclass or something else

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

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

class SortedInventory (Inventory):

    def add_item(self, item):
        super().add_item(item)

1 Answer

Michael Hulet
MOD
Michael Hulet
Treehouse Moderator 47,024 Points

A normal Inventory is not sorted. However, a SortedInventory is. In order to guarantee this, you need to re-sort the list each time a new item is added. A Python list already has a built-in method called sort, which sorts the list. For example:

numbers = [3, 5, 2, 4, 1]
numbers.sort()
print(numbers) # prints [1, 2, 3, 4, 5]

All you need to do is call this method on slots to sort it after you add an item to it