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!

Travis John Villanueva
Travis John Villanueva
5,052 Points

Super! 3/3 - Im lost

Hi Team

I think im lost regarding this problem. The problem require a sorted list after adding an item using a super class method inheriting to its child class. I have applied the inheritance properly and my Sortedinventory inherits that method properly but i dont know how to proceed further as i have this query on my head

My list of problem:

  1. Once items are added, i should call the slots list to perform slots = slots.sort() however, i dont know where would i call it.
  2. I have the thought that once i perform the class SortedInventory(Inventory) - did i also call automatically the self.slots
  3. So, my understanding is this should be easy, call the list that contains the added items can you provide me with a step by step logic?
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(item)

2 Answers

Jordan Hoover
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jordan Hoover
Python Web Development Techdegree Graduate 59,268 Points

I'm not totally clear on your question, but I think you're overthinking it. Just sort the property after the super method has been called. Make sense?

seth schwan
seth schwan
1,488 Points

Part 2 asks you to add items into the "slots" list. Which looks like this:


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

In part 3, you actually have to replace the "self.slots.append(item)" with "self.slots.sort()". Rather than adding "self.slots.sort()" to the already existing code from the previous part of the challenge. It took me a while to figure this out. Hope that helps :)