Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: