Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Inheritance Super!

shubhamkt
shubhamkt
11,675 Points

Super Duper

Can't figure out how to access the slots

inventory.py
class Inventory:
    slots=[]
    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)
    def __init__(self):  
        super().__init__()
        slots.sort()

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

You are very close. You do not need to override the __init__ method. Task 3 want you to sort the list of items after an item has been added. The addition happens already in the add_item method. You need only to add the current slots attribute.

The attribute slots is inherited from Inventory so it is available as self.slots (the same as is done in Inventory). So a simple self.slots.sort() should suffice.

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

Jeremy Schaar
Jeremy Schaar
4,728 Points

Hmm. That's what I thought would work, but the following isn't passing. I get "Bummer! Hmm, the items don't seem to be sorted"

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)
        self.slots.append(item)
        self.slots.sort()
Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

You are very very close! The overridden add_item method does not need the append operation since the call to super covers the append. Remove the extraneous append and it should pass.

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