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!

Lee McCormick
seal-mask
.a{fill-rule:evenodd;}techdegree
Lee McCormick
Python Web Development Techdegree Student 16,595 Points

not sure what to do on this

i think im doing what is in the video but this is not descriptive enough

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

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

class SortedInventory(Inventory):
    pass

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

1 Answer

If you need to pass the second part of the challenge, you need to delete "pass" and keep the rest of the code. It passes the challenge, be careful though with indentations and etc. You miss the colon after add_item function in the SortedInventory

 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)
David Capella
David Capella
4,126 Points

Quick question, Why don't you have to pass self in super().add_item(item)?

It is a very interesting question. I checked possible forums/resources and etc. So, the primary function of the super() in simple words:

" The great thing about super is that it can be used to enhance any module method. Plus, there’s no need to know the details about the base class that’s being used as an extender. The super function handles all of it for you.

So, for all intents and purposes, super is a shortcut to access a base class without having to know its type or name."

http://www.pythonforbeginners.com/super/working-python-super-function

when you call super() without any arguments you refer to the base class, the first class which could be a parent for a lot of instances. Every Instance is also a class and could be a parent. If you want to create a new Instance of the Instance of the first primary class and want to refer to the methods exactly of that instance, you need to pass the name of that instance and "self". If you don't you will always refer to the first primary class. As soon as we work only with one primary class and first "generation if Instances", we don't need additionally refer with "self" to that Primary class.

It is how I understand that. I recommend you to read some tutorials as well and if you have something to add, please share.

Kenneth Love , if you see this discussion, couldn't you please comment as well, if we are correct?