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!

Keenan Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
Keenan Smith
Python Development Techdegree Student 5,487 Points

Stuck on SortedInventory class

looking in my notes I thought this is what it is asking us to do. I keep getting a bummer error though. What am I doing wrong?

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(self, item)
        self.slots.append(item)

1 Answer

Steven Parker
Steven Parker
231,198 Points

The instructions ask you to "override the add_item method.", but this code is overriding "__init__" instead.

Also, you won't need to call "append" yourself, since it will be done by the "super" call.

Keenan Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
Keenan Smith
Python Development Techdegree Student 5,487 Points

Ok, so I think now I understand what it was asking for when it said override. Thank you. now after fixing that and eliminating the append code, im getting an error that says: "Bummer: You need to override add_item inside of SortedInventory. For now, just have it call super()"

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

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

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

Is it not overriding even though I'm creating the method add_items?

Steven Parker
Steven Parker
231,198 Points

You need to "override the add_item method.", but this code is defining "add_items" (plural) instead.

And when you call a method, you don't need to pass "self" explicitly. The system adds that for you.

Keenan Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
Keenan Smith
Python Development Techdegree Student 5,487 Points

That worked out. had that not been a challenge could we just have sorted the list within the Inventory class? Or is it better to have a new class do it?

Steven Parker
Steven Parker
231,198 Points

You could have added sorting to the original class, but maybe there would be a reason to retain the original order of creation (it's not clear from just the code).