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

frustration.py

I'm not sure if i needed to first create an init method for the instance so I didn't border. I'm assuming that since the Liar class is extended with list, it will function as a list object from the start.

So in my code, i wanted the length of the argument to be 5 but i think i missed something crucial. Please help.

Thank you

frustration.py
class Liar(list):

    def __len__(self, p):
        super().__len__(p) = 5
        return len(p)

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Idris,

The __len__ method doesn't take any arguments (except self). Remember that this method is acting on a list, so it already knows (from self) what object it is checking the length of. This might be a bit counter-intuitive, because we call len() by passing an argument into it, but it's good to remember that often the definition of a dunder method doesn't look exactly like its non-dunder counterpart. When in doubt, just check the docs to see what that particular method takes. For example: __len__.

This applies both to your method definition and, correspondingly, to your calling of super().

This brings us to the next issue. In your line where you call super() you are trying to assign a value to it. However, you can't assign a value to the result of a method call.

I think what you are trying to do is always return 5, regardless of what the true length is. If so, you wouldn't need to call super() at all, you could just write:

def __len__(self):
    return 5

But now we run into a problem: what if the length really is 5? Then your Liar class is no longer lying about the length.

The solution is easy enough: we figure out what the length really is first (by calling super()) then if that value is 5, we return anything that isn't 5. If the value in super() isn't 5, then you can return 5 just like in the previous code snippet.

Hope that clears everything up for you.

Cheers

Alex

Hi Alex, you are the best! Thanks so much. That gave me some sleepless moments. I used your hints and fixed it as below, and boom I passed the challenge!

class Liar(list):

def __len__(self):
    super().__len__(self)
    if len == 5:
        return 2
    return 5