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!

Matt Otis
Matt Otis
1,402 Points

Not so super(), need help with Challenge 2 of 3 in inventory.py

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

Not really sure what theyโ€™re asking and why; and therefore, not sure why overriding the add_items in the child object is even necessary since weโ€™re just doing the same thing. And really not sure what the Check Work is wanting

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

Whatโ€™s it really asking, and how do I do it?

3 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Matt,

In this second step of the three-step challenge, you're being asked to override the superclass's method. That's pretty much what you've done in your test code, except that you're calling super's version of add_item and passing it self. Remember, in Python, every time we create a method in a class we give it the first parameter of self, but we never pass self as that first argument when we call a class's method.

For now, all we're doing is creating the overridden method, without actually implementing the unique behaviour of that overridden method (kind of like how in step one, you created the subclass without implementing any of the methods).

Making that fix will get you to the third step of the challenge where your question as to why you're overriding add_item will be answered.

Hope that clears everything up for you.

Cheers

Alex

Ohhhh I think looking at this conversation and Matt's eventual answer may have helped me figure this out. But if I may, just wanted to confirm that here. I had the same problem with question #2 of this challenge problem. And then question #3 as well actually haha.

So regarding #2 (and super in general honestly) when we call super its simply referring us to the method within the subclass's superclass? So in this case by using super its simply calling and running the add_item method from the Inventory superclass - hence the necessity to not use self?

Then in question #3 when it asks us to sort the slots we're taking our subclass method of add_item and then increasing the next line. So basically using super we're calling the previous add_item method and then we add a new line to sort, and because we've used super we've now overwritten the super class method of add_item?

Thanks for any help you can provide!

Matt Otis
Matt Otis
1,402 Points

Thank you!

Also found that I dropped my colon...much better to do in programming than IRL

Code that worked...

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)
        #Code to add for 3 of 3
        list.sort(self.slots)

This Code runs perfectly

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

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


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