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
Sarah Burgart
5,159 Pointsmethod on list called through super()
Not understanding why append() worked as expected and sort() did not?
class Inventory:
def __init__(self):
self.slots = []
def add_item(self, item):
self.slots.append(item)
class SortedInventory(Inventory):
def __init__(self):
super().__init__()
def add_item(self, item):
super(SortedInventory, self).add_item(item)
self.slots.append(item)
self.slots.sort()
3 Answers
Alex Koumparos
Python Development Techdegree Student 36,888 PointsHi Sarah,
Let's begin by eliminating some harmless but redundant code so we can focus on the problem area with your method.
SortedInventory doesn't extend or override any functionality from Inventory's __init__ method. Since subclasses automatically inherit their parent's methods and attributes (including __init__), we can omit the whole method.
Also note that when using super inside a method, you can omit the arguments, as the compiler will correctly retrieve the class and instance.
These changes would simplify your class to:
class SortedInventory(Inventory):
def add_item(self, item):
super().add_item(item)
self.slots.append(item)
self.slots.sort()
Now that our code is more readable, we can step through what your extended add_item method is doing when we call it.
We're going to assume for our illustration that we already have a SortedInventory object assigned to my_inv
with the slots attribute being ['axe', 'health potion', 'shield'].
Here's our method call:
>>> my_inv.add_item('mana potion')
And we step through the execution of your method line by line:
-
super().add_item(item)
We're callingsuper, which Python looks up asInventory. This means we are usingInventory'sadd_itemmethod (butselfstill refers tomy_inv):
1.1. (insideInventory.add_item)self.slots.append(item)
We're appendingitem('mana potion') tomy_inv.slots. Thus,my_inv.slotsis now['axe', 'health potion', 'shield', 'mana potion'].
We've reached the end ofInventory's version of the method, so we return back toSortedInventory'sadd_itemmethod. -
self.slots.append(item)
We're appendingitem('mana potion') tomy_inv.slots, so it is now['axe', 'health potion', 'shield', 'mana potion', 'mana potion'] -
self.slots.sort()
We're using thesortmethod for lists to produce:['axe', 'health potion', 'mana potion', 'mana potion', 'shield']
We can verify this by querying the slots attribute:
>>> my_inv.slots
['axe', 'health potion', 'mana potion', 'mana potion', 'shield']
Working through the code line by line, it should be clear that we are appending mana potion twice: once during the super call to Inventory and then again inside SortedInventory's own add_item method.
We can fix the code by removing the append line in your subclass's method. Remember, the reason that we are taking advantage of inheritance and extending the superclass's method (rather than simply writing a whole new independent class) is so that we can continue to rely on the superclass to perform its existing functionality (simply adding items to slots) but then add our own additional functionality after the superclass has done its thing (sorting the slots).
Hope that clears everything up for you.
Cheers
Alex
Brian Chalfant
8,720 Pointssuper(SortedInventory, self).add_item(item)
This is where your problem lies. You don't need to call to super because your SortedInventory.add_item is already overriding your Inventory.add_item. Take that line out and it should work just fine.
Sarah Burgart
5,159 Pointscan't. it specifically says, "Bummer: You need to override add_item inside of SortedInventory. For now, just have it call super().
Anthony ugwu
672 PointsYou can call the superclass add_item method under your own add_item using super()
def add_item(self, item):
super().add_item(item)
Sarah Burgart
5,159 Pointsisn't that what I did?
def add_item(self, item): super(SortedInventory, self).add_item(item) self.slots.append(item) self.slots.sort()