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!

J fori
seal-mask
.a{fill-rule:evenodd;}techdegree
J fori
Python Development Techdegree Student 200 Points

why we should not pass self as an argument?

in this example if i want to pass a self as argument

super().add_item(self,item)

i Got Run time Error: TypeError: add_item() takes 2 positional arguments but 3 were given

so what should i do to pass a self as argument explicitly?

And when we know, we should pass a self as Argument and when python handles self by its own?

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

    def add_item(self, item):
        self.slots.append(item)
class Inventory:
    def __init__(self):
        self.slots = []

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

class SortedInventory(Inventory):

 #Overriding Method 
    def add_item(self,item):

        super().add_item(self,item)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, J fori! You have a child class that's inheriting from a parent class. The parent has access to itself, and the child has access to itself, but the parent doesn't need to know anything about the child to execute its functions. If you pass in self from the child to the parent, you're passing in the instance of the child to the parent. So now the parent is getting information it simply doesn't need. The add_item method on the parent takes itself (not the child) and an item.

Hope this helps! :sparkles: