Python Object-Oriented Python Inheritance Super-Duper!

Hai Phan
Hai Phan
2,442 Points

I have a problem with challenge 3, please help

This is my code

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)
        super().slots.sort()

I really didn't understand the question so I try things in my way and I think I still don't know about super(), self, relations between classes. Could anyone explain these thing to me? Thank you for advance.

1 Answer

Steven Parker
Steven Parker
203,995 Points

You're really close there, and it looks like you mostly have the right idea; but you don't need to call "super()" to access class variables. Just put "self." in front of the name:

        self.slots.sort()

So use "self." for variables, and "super()" for methods.

Hai Phan
Hai Phan
2,442 Points

Yeah it works, thank you :)