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 Advanced Objects Frustration

Joel Ulens
Joel Ulens
3,101 Points

I am honestly completely stuck. How do i fix this?

So I have tried this on an IDE to see which error it gives and it gives, this error: File "python", line 1, in <module> File "python", line 4, in len File "python", line 4, in len File "python", line 4, in len [Previous line repeated 328 more times] RecursionError: maximum recursion depth exceeded while calling a Python object

I have tried multiple different methods and none of them work

frustration.py
class Liar(list):
    def __len__(self):
        super().__len__()
        self = len(self)
        return self*2

1 Answer

andren
andren
28,558 Points

The biggest issue with your current code is that you call len(self). The __len__ method you are currently writing is the method that gets called when len is called on an object. That means that by calling len(self) inside of __len__ you end up with an infinite loop of the __len__ method calling itself over and over.

You don't really need to do anything to the self parameter in this method. When you call super().__len__() you are calling the original __len__ method, which means that whatever number that method returns is the real length of the object. Once you know the real length you can simply add some random number to that, and you will end up with a lying __len__ function.

Like this for example:

class Liar(list):
    def __len__(self):
        return super().__len__() + 2 # Take what the original len functions returns and add 2

It doesn't really matter what number you add to it, as long as it causes your custom __len__ method to return a different number than the original __len__.

Edit:

It's worth mentioning that while the number you use does not matter you do need to use addition, not subtraction, multiplication or division. This is due to the fact that the challenge sends you a list that is actually empty. That means that the original __len__ function returns 0. Subtracting from 0 will result in a negative number which is an invalid value for __len__ to return. And multiplying or dividing with 0 as the starting number always results in 0 being the result, which means the function would not actually be lying about the length.

andren, thanks for the explanation about multiplication. That did trip me up for this challenge.

Thank you for this explanation. It is the best one for this Frustration exercise!