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!

Bill Gardner
Bill Gardner
9,518 Points

Super! inventory.py (part2) I don't think I understand what the question is asking me to do?

It's just not making sense to me!

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(self, item)
        self.slots.append.(item)
Leroy Saldanha
Leroy Saldanha
8,006 Points

class SortedInventory(Inventory):

def add_item(self,item):
    super().add_item(item)

20 Answers

Jay Norris
Jay Norris
14,824 Points

The only way I can get a correct answer is with the code below. But it can't be right because I don't use super(). If I try to use super(), it fails. The error messages could be a little more helpful.

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)

EDIT: The above is for part 2. If I pass part 2 with incorrect code, does it set me up for failure on part 3?

Dan B
Dan B
6,155 Points

The proper answer is from Timothy Sawyer below. Scroll down a bit folks and analyse the code he shared carefully.

Ankoor Bhagat
Ankoor Bhagat
1,098 Points

The course is not well put together. It lacks good examples for challenges. I was able to understand basics of Python OOP with better explanations from this series of videos: https://www.youtube.com/watch?v=ZDa-Z5JzLYM&list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc

thank you! Im having a really hard time understanding Kenneth on this section

Luis Bressoud
Luis Bressoud
4,367 Points

this video was super helpful! much easier to understand. thanks

Jean Santana
Jean Santana
9,036 Points

Thank you! the video was very helpful, everything make sense now!

this video has nothing to do with super() i still dont understand super()

How ironic I had to watch this series to pass Treehouse code challenges. https://www.youtube.com/watch?v=Cn7AkDb4pIU <------ This is helpful as well

Kenneth chooses some series of a subject he likes instead of something practical. So it never makes sense

Unfortunatly I have to agree. I'm doing the begginer Python course and I had no problem understanding the concepts untill now. I'm really struggling with this one...

Timothy Sawyer
Timothy Sawyer
31,052 Points

This worked for me.

class SortedInventory(Inventory):
    def add_item(self, item):
        super().add_item(item)
        self.slots.sort()
Dan B
Dan B
6,155 Points

This should be chosen as the best answer. It's clean and simple and it passes the challenge. Take my upvote,

Ben Hendricks
seal-mask
.a{fill-rule:evenodd;}techdegree
Ben Hendricks
Python Web Development Techdegree Student 15,217 Points

I agree with others that unfortunately this portion of the course is very poorly explained. Perhaps better examples would help clarify and explain the subject better.

Travis Bailey
Travis Bailey
13,675 Points

Hey Bill, Alex is referring to part 3 of the question, part 2 is dealing with super().

I had to watch the video 'Super-Duper' (the one before this code challenge) a few times before I even grasped the basics of how super() works.

In terms of the challenge, it's just asking you to override the 'add_item' method from the Inventory class. Since you're overriding the method, you would have to re-write the code, or use super() to regain the functionality you over wrote. Since this code challenge deals with small pieces of code, it's hard to see the benefit.

Basically, let's say the 'add_item()' method from Inventory your SortedInventory class inherited had about 50 lines of functionality to it. However that 'add_item()' method doesn't have the ability to take in an argument to say what numeric order it should be placed (1st, 2nd, or 3rd). So I would override 'add_item()', add the 'num_order' parameter, then call 'super()' to bring in the functionality from Inventory's 'add_item()' method back.

Here's the example code from the video that shows super() in use. If it makes you feel any better, I'm still trying to wrap my head around super() myself.

import random

class Character:
    def __init__(self, name, **kwargs):
        self.name = name

        for key, value in kwargs.items():
            setattr(self, key, value)


class Thief(Character):
    sneaky = True

    def __init__(self, name, sneaky=True, **kwargs):
        super().__init__(name, **kwargs)
        self.sneaky = sneaky

    def pickpocket(self):
        return self.sneaky and bool(random.randint(0,1))

    def hide(self, light_level):
        return self.sneaky and light_level < 10

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.item=item
    self.slots.sort()
    pass

"""This solution work-out for me which include task 3 with super()"""

Bill,

You're right that the question itself is a bit confusing. It's asking for override a method, but nothing to override and has to use super() to regain the functionality. I also did a bit search before I can pass it as below. I think this test is only asking to practice the syntax.

class SortedInventory(Inventory):
    def add_item(self,item):   # this line satisfy the need of overriding the "add_item" function in child class
        super().add_item(item)  # this line calls super(), which regains the functionality defined in parent class.

By Christopher Shaw

Super allows you to call a function from the parent class. So effectively we are calling (running) the add_item function in the parent class. And sending it the item.

def add_item(self, item):
        super().add_item(item)

This was my answer:

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)

Part 2 says to sort the inventory.

You can use the .sort() method, like this:

I'm pretending that I'm in the Python shell

>>> a = [2, 3, 1]
>>> print(a)
[2, 3, 1]
>>> a.sort()
>>> print(a)
[1, 2, 3]

I hope this helps :grin:

:dizzy: ~Alex

Bill Gardner
Bill Gardner
9,518 Points

Hi Everybody, Thanks so much for your help and explanations, Alex and Travis! I think that there may be some sort of issue with the problem itself, because I find myself in Jay's situation. I've tried every combination of over riding the method that I could think of and the only way that it worked was to remove the super() method. Step 3 worked just fine without it.

YURI PAPINIAN
YURI PAPINIAN
11,838 Points

IMHO the problem with this task is that it uses a bad example of a super() function. Basically, all the task is asking for can be done by simply inheriting the Main Class.

If I am wrong please explain.

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)

pleade note : i had to skip a line before defining add_item in SortedInventory., otherwise it's a bummer

Bill Gardner
Bill Gardner
9,518 Points

Thanks for your reply, Alex. It's asking me to sort?

This is the instructions: "Great! Now override the add_item method. Use super() in it to make sure the item still gets added to the list."

I apologize for still being confused.

Bill

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.item=item self.slots.sort() pass

Mark Pertsovsky
Mark Pertsovsky
6,787 Points

You can do as follows: (it worked for me)

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

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

class SortedInventory(Inventory):
    def add_item(self, item, **kwargs):
        super().add_item(item)
        for key, value in kwargs.items():
            itm = key, value
            self.slots.append(itm)
Matej Sever
Matej Sever
4,810 Points

This also works. I guess it already appends in parents class?

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

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

class SortedInventory(Inventory):
    def add_item(self, item, **kwargs):
        super().add_item(item)
         list.sort(self.slots)

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)
    super()
Euenlee Tan
Euenlee Tan
2,165 Points

Where does the "slots" come in from? At the "class" of Inventory, the only parameter taken in was (self), there were no slots! Immediately after this, there's the code "self.slots=slots", you can do this even without **kwargs??

Shavez Memon
seal-mask
.a{fill-rule:evenodd;}techdegree
Shavez Memon
Python Development Techdegree Student 2,452 Points

This worked for me:

class SortedInventory(Inventory):

def add_item(self, item):

    self.slots.append(item)

    super().add_item(item)