Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Inheritance Super!

Arjun Amin
Arjun Amin
2,228 Points

I have no idea what to do

I have been asked:

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

Please assist me through the whole process because I don't understand the question.

Thanks.

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

    def add_item(self, item):
        self.slots.append(item)
class SortedInventory(Inventory):
    pass

5 Answers

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,236 Points

Super allows you to call a function from the parent class. So effectively we are calling (running) the add_item function in the parent class. And sending it the item.

def add_item(self, item):
        super().add_item(item)
nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

I guess at this point I don't understand why I wouldn't include self like this:

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

Hmm... I'm still not getting this. Since we've already made SortedInventory a subclass of Inventory, then why do we need to use super to call the add_item method? Isn't the whole point of making it a subclass to pass all the methods down from the parent?

At least at this point we haven't changed that method, correct? So is this just the first step in altering the add_item method for the SortedInventory class? Meaning, if we weren't planning to change this method we wouldn't be defining add_item in the SortedInventory class?

GLEB CHEMBORISOV
GLEB CHEMBORISOV
11,182 Points

Try this: class SortedInventory(Inventory):

def __init__(self, slots=[]):
    super().__init__()
    self.slots=slots

def add_item(self,item):
    self.slots.append(item)
Qiong Li
Qiong Li
4,192 Points

class SortedInventory(Inventory): def add_item(self, item): super(SortedInventory,self).add_item(item)

class SortedInventory(Inventory): def add_item(self,item): super().add_item(item)

I would have to agree with everyone else. This example is extremely contrived and makes no sense to do. But this worked:

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)