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!

Sabine Lazuhina
Sabine Lazuhina
2,334 Points

overriding parent methods

what is wrong with child class?

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

2 Answers

Richard Li
Richard Li
9,751 Points

Are you trying to do this?

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

In your code, super() which represent the parent class is called, but the problem is that there is no append() methods in the parent class, so there causing the error. append() is actually the methods that comes with slots attributes, not the Inventory class.

However, in the parent class there is an add_item() methods that you can call, so in the above code, the operation flow is:

  1. An instance of SortedInventory called the add_item method in the subtclass
  2. Python first search for "add_item" in the subclass and indeed find it
  3. So Python use that add_item in the child class, in which calls super().add_item which refer to the method in the parentclass
  4. Do what needs to be done.

To override the add_item methods:

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

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

class SortedInventory(Inventory):
    def add_item(self, item):
        self.slots.append(item * 2)
outputs:
>>> bag = Inventory()
>>> small_bag = SortedInventory()
>>> bag.add_item('milk')
>>> small_bag.add_item('milk')
>>> bag.slots
['milk']
>>> small_bag.slots
['milkmilk']

here python the subclass methods. Although there is a method in the parentclass which have the same name of this one, Python already find what it wants in the closest namespace. Thus the different output.

Official python docs about single class inheritance:

if a requested attribute is not found in the class, the search proceeds to look in the base class(parent class). This rule is applied recursively if the base class itself is derived from some other class.

Method references are resolved as follows: the corresponding class attribute is searched, descending down the chain of base classes(parent class) if necessary, and the method reference is valid if this yields a function object.

Hope all these helps for you to understand the flow of class inheritance.

Kent ร…svang
Kent ร…svang
18,823 Points

You have whitespace before "append(item)" on your last line. Removing that should be enough for this to run properly