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!

How to add super to add_item method

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): pass

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):
            pass

1 Answer

Steven Parker
Steven Parker
231,271 Points

You're close, but:

  • when you call the super's "add_item", you don't need to pass "self" as an argument
  • the line calling the super's "add_item" should not have a colon at the end
  • you don't need "pass" anymore now that you have a method

Thank you for your helpful answer but why don't we need to pass self as an argument in super

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Here's a link that may shed some light on that for you.

https://www.codecademy.com/forum_questions/547e29f79c4e9d958e0049db

https://stackoverflow.com/questions/36557009/explicit-passing-of-self-when-calling-super-classs-init-in-python

It's not required to pass the self parameter when calling super(). My understanding of this is that if you don't want to refer to the parent class, you wouldn't pass self to super.

Steven Parker
Steven Parker
231,271 Points

It's just like any other method call, you don't explicitly pass "self", but the system provides it to the method as the first parameter implicitly.

thank you