Python Object-Oriented Python Advanced Objects Frustration

Brian Boring
PRO
Brian Boring
Pro Student 3,903 Points

Answered correctly, but one thing confuses me

Let me start by saying I have passed this challenge, so I don't need the correct answer, I just am curious about why my first attempt did not work.

This challenge asks to override the len method and change the result so that len is incorrect.

As you can see in my comments, I originally returned super().len() * 2. I qualified it in my IDE (PyCharm) and it was multiplying the length of the list by 2, but still it wouldn't pass in Treehouse.

I spent 30 minutes chasing my tail and trying to see where I went wrong. In a last ditch effort, instead of multiplying it by two, I added 5 to it, and it passed.

Why would that make any difference? The only thing the challenge asked was that it change the length so that it is not accurate to the original length of the list. Any thoughts?

frustration.py
class Liar(list):
    def __len__(self):
        return super().__len__() + 5

#Originally had return super().__len__() * 2

3 Answers

andren
andren
28,359 Points

If you use return super().__len__() - 1 you get an error message that len cannot return a value under 0. That indicates that the list Treehouse passes in contains 0 objects. Since 0 * 2 = 0 your function would not actually be lying about the length of the list.

Quinton Dobbs
Quinton Dobbs
5,149 Points

My god, it's so simple!

William Harrison
William Harrison
9,462 Points

I'm not entirely should, but the challenge says ALWAYS return a false value. Treehouse data sets, are crafted to make things difficult. If the data set had a 0 or Null length, your first attempt would return the correct answer.

Brian Boring
PRO
Brian Boring
Pro Student 3,903 Points

That's a really good point you both bring up. I'll bet that is it. Thanks to you both. I'll be able to sleep tonight now. ;)