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.
class Liar(list): def __len__(self, p): super().__len__(p) = 5 return len(p)
Alex KoumparosPython Web Development Treehouse Moderator 32,453 Points
__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
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.