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 trialMichael Nanni
7,358 PointsCan I increment inside the dunder len function? Looking for some direction with the frustration.py challenge.
I'm really struggling with the instructions here. I'm not sure how to use super() here unless maybe I make a class called list, and inherit from that?
Seems like I could just increment the dunder len function and return that result. Am I on the right track here?
Thanks in advance for the help.
class Liar(list):
super().__init__()
def __len__(self, numlist=None):
self.numlist = []
if self.numlist > 0:
return len(self.numlist) += 3
3 Answers
Steven Parker
231,122 PointsYou can't increment the "len" function itself ("+=
"), but you can always add to the value it returns ("+
").
Also, since you're redefining "len", to avoid a recursive infinite loop you probably want to call the parent version instead (using "super()
").
Lastly, since the object derives from "list", you won't need to create a separate "numlist". You can just use "self".
Michael Nanni
7,358 PointsJust getting back to this and wanted to follow up. Anyone have some thoughts on my code above?
Steven Parker
231,122 PointsSure:
- you only need to override "
__len__
" (not "__init__
") - that second-to-last line ("
__len__(self)
") isn't correct syntax (and not needed) - take another look at my previous hints (about using "super")
Michael Nanni
7,358 PointsWell I passed the challenge but I don't understand what's happening in the code.
Why do we need to call the len function of the parent class using super()? Could we not override the len method to meet the needs of the challenge?
class Liar(list):
def __len__(self):
truth = super().__len__()
return truth + 2
Steven Parker
231,122 PointsThe only way to be certain that you generate a wrong answer, is to get the right one (using "super") and alter it.
Michael Nanni
7,358 PointsMichael Nanni
7,358 PointsI get that I can increment the return value. That much makes sense, but I don't understand anything after that point. Here's what I know:
I am creating a class that inherits from list, which is the parent class, and I know nothing about it. At least it's some built-in class right?
The instructions use an example of a list, which to me implies that I need an actual list variable no? How can I return a value from a variable/attribute that doesn't exist? Am I evaluating for true/false or an integer?
Is the parent version of len() the len() function? Am I using it with super() and then redefining later like this?