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 trialJennifer Lithgow
11,778 PointsFrustration.py
I am super lost with this challenge. I can follow along with the examples in the preceeding video, but I have no idea how I could alter len to give a wrong number. I don't even know where to begin on this one. In IDLE, tried to even just set it up and put in "pass" just to see if I could get len to work WITHOUT altering it, and I couldn't get anything to run. "Frustration" is an appropriate name for this challenge. :(
class Liar(list):
def __len__(self):
super().__len__()
return self
10 Answers
Kenneth Love
Treehouse Guest TeacherIf you call super().__len__()
(and assign it to a variable, of course), you'll get the length of self
(well, the instance, but that's the name inside the method), using __len__
from the parent class (list
). Using that value, you can change it so that it's wrong. Add 1, remove 1, multiply by 2, whatever. Then return that new value.
Alexander Davison
65,469 PointsYou can just return 2
. That worked for me.
Jennifer Lithgow
11,778 PointsPerfect, thank you!
Kenneth Love
Treehouse Guest TeacherBooooo! :D
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 PointsHAHA! That's terrible.
Qasa Lee
18,916 Pointsclass Liar(list):
def __len__(self):
return super().__len__() + 1
Check this out, good luck !
Leroy Saldanha
8,006 Pointsdoesn't work
Nicholas Lim
Python Development Techdegree Student 6,055 PointsOK everybody here's the answer:
import random
import math
class Liar(list):
def __init__(self, *arg):
self.arg = []
def __len__(self):
super().__len__()
randomNum = random.randint(2, 9)
x = len(self.arg) + math.factorial(randomNum)
return x
Nicholas Lim
Python Development Techdegree Student 6,055 PointsWhen you paste that in it will be correct. So mark the differences between yours and mine. Hopefully this helps.
Jay Norris
14,824 PointsI'm still lost. Am I going in the wrong direction here?
def Liar(list):
def __len__(self):
length = super().__len__()
lie = length * 2
return lie
Kenneth Love
Treehouse Guest TeacherYou're really close. First, how do you define a class?
Second, it needs to always return a wrong length. What length would your current version give if there wasn't anything in the list?
Cristian Romero
11,911 Pointshmm even after adding class this still doesn't work?
Jay Norris
14,824 PointsThat worked! I was never going to notice that "def"
Thank you!
Tomas Svojanovsky
26,680 Pointsimport random
from operator import add, sub
class Liar(list):
def __len__(self):
ops = (add, sub)
op = random.choice(ops)
length = super().__len__()
return op(length, 3)
Nathan English
Front End Web Development Techdegree Student 10,817 PointsThis post is pretty old, but I'm just gonna leave this here.
import random
class Liar(list):
def __init__(self,*arg):
self.arg = []
def __len__(self):
super().__len__()
randomNum = random.randint(0,10)
x = randomNum + len(self.arg)
return x
Tapiwanashe Taurayi
15,889 Pointsimport random import math
class Liar(list): def init(self, *arg): self.arg = [] def len(self): super().len() randomNum = random.randint(2, 9) x = len(self.arg) + math.factorial(randomNum) return x
sonny unverferth
Python Web Development Techdegree Student 2,889 PointsHey Ken, I have followed the python course from the beginning without skipping anything. Im stuck on this Challenge and have never seen _ _ len _ _ or .super() before. Its really frustrating. Any advice?
Temiye Oluseun
2,902 PointsHey try this class Liar(list): def len(self): length = super().len() wrong = 2 return wrong
Jennifer Lithgow
11,778 PointsJennifer Lithgow
11,778 Points"and assign it to a variable, of course" ooooooohhhhh, okay. That seems so simple, now! I don't know why I wasn't able to wrap my head around that before. (Too many lectures in a row, maybe? Ha ha ha!) That's super helpful. I'll go back and give it another go. Thank you!
Sharla Kew
15,356 PointsSharla Kew
15,356 PointsCould you elaborate a bit on when you would choose to set super().whatever (or just the magic method in the case of immutable types) as a variable and when you wouldn't?
ex:
vs
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherSharla Kew it all depends on what the method does.
In this example, calling the
super()
version of the method changes the instance and doesn't return anything so it's still effective to use it.With
__len__
, though, it doesn't set anything on the instance and it returns a value. That return value needs to go somewhere, so we put it into a variable.Sharla Kew
15,356 PointsSharla Kew
15,356 PointsKenneth Love Ok so -
Some methods, what they do is set a variable, so why would you set a variable equal to the setting of a variable? ex: the set_name() method in your example.
Other methods, what they do is return a value, and you gotta store that in a variable or it vanishes into the ether. ex: __len__()
Putting super() on the beginning of them made them seem spooky but they need to be treated like normal methods.
Nice. Thanks Kenneth!