Sarah Burgart5,159 Points
method 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()
Alex KoumparosPython Web Development Treehouse Moderator 33,515 Points
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
__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
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, which Python looks up as
Inventory. This means we are using
selfstill refers to
'mana potion') to
['axe', 'health potion', 'shield', 'mana potion'].
We've reached the end of
Inventory's version of the method, so we return back to
'mana potion') to
my_inv.slots, so it is now
['axe', 'health potion', 'shield', 'mana potion', 'mana potion']
We're using the
sortmethod for lists to produce:
['axe', 'health potion', 'mana potion', 'mana potion', 'shield']
We can verify this by querying the
>>> 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
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.
Brian Chalfant4,379 Points
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.
You can call the superclass add_item method under your own add_item using super()
def add_item(self, item): super().add_item(item)