Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Frustration

Michael Nanni
Michael Nanni
7,358 Points

Can 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.

frustration.py
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
Steven Parker
210,936 Points

You 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
Michael Nanni
7,358 Points

I 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?

class Liar(list):
    def __init__(self):
        super().__len__(self)
    def __len__(self):
        __len__(self)
            return __len__(self) + 1 
Michael Nanni
Michael Nanni
7,358 Points

Just getting back to this and wanted to follow up. Anyone have some thoughts on my code above?

Steven Parker
Steven Parker
210,936 Points

Sure:

  • 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
Michael Nanni
7,358 Points

Well 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
Steven Parker
210,936 Points

The only way to be certain that you generate a wrong answer, is to get the right one (using "super") and alter it.