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 trialBill Gardner
9,518 PointsSuper! 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!
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)
20 Answers
Jay Norris
14,824 PointsThe 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
6,155 PointsThe proper answer is from Timothy Sawyer below. Scroll down a bit folks and analyse the code he shared carefully.
Ankoor Bhagat
1,098 PointsThe 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
Lauritz Patterson
1,207 Pointsthank you! Im having a really hard time understanding Kenneth on this section
Luis Bressoud
4,367 Pointsthis video was super helpful! much easier to understand. thanks
Jean Santana
9,036 PointsThank you! the video was very helpful, everything make sense now!
anthony pizarro
1,983 Pointsthis video has nothing to do with super() i still dont understand super()
Javano Collins
9,108 PointsHow ironic I had to watch this series to pass Treehouse code challenges. https://www.youtube.com/watch?v=Cn7AkDb4pIU <------ This is helpful as well
David Ryan
Courses Plus Student 14,981 PointsKenneth chooses some series of a subject he likes instead of something practical. So it never makes sense
Stefano Silva
2,340 PointsUnfortunatly 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
31,052 PointsThis worked for me.
class SortedInventory(Inventory):
def add_item(self, item):
super().add_item(item)
self.slots.sort()
Dan B
6,155 PointsThis should be chosen as the best answer. It's clean and simple and it passes the challenge. Take my upvote,
Ben Hendricks
Python Web Development Techdegree Student 15,217 PointsI 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
13,675 PointsHey 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
nishitbodawala
5,924 Pointsclass 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()"""
Junyi Shui
1,303 PointsBill,
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.
Robert Stepanyan
2,297 PointsBy 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)
John Perry
Full Stack JavaScript Techdegree Graduate 41,012 PointsThis 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)
Tylan Coleman
Python Development Techdegree Student 5,186 PointsThis is my favorite answer! The "self" is not needed while using the super.
Alexander Davison
65,469 PointsPart 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
~Alex
Bill Gardner
9,518 PointsHi 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
11,838 PointsIMHO 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.
Clemence Ngwenya
24,178 Pointsclass 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
9,518 PointsThanks 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
Team Voywing
18,181 Pointsclass 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
6,787 PointsYou 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
4,810 PointsThis 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)
Manuel Prince Da Fonseka
2,920 Pointsclass 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
2,165 PointsWhere 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
Python Development Techdegree Student 2,452 PointsThis worked for me:
class SortedInventory(Inventory):
def add_item(self, item):
self.slots.append(item)
super().add_item(item)
Leroy Saldanha
8,006 PointsLeroy Saldanha
8,006 Pointsclass SortedInventory(Inventory):