Python Object-Oriented Python Advanced Objects Frustration

Leo Yun Tao
Leo Yun Tao
13,467 Points

TypeError: __len__() missing 1 required positional argument: 'item'

I am trying to do a challenge but when I try to run that code it says TypeError: len() missing 1 required positional argument: 'item', so what does that error mean and how do I fix my code?

frustration.py
class Liar(list):
    def __len__(self, item):
        try:
            return self[item]
        except:
            return super().__len__(item) + 2 or super().__len__(item) -3

1 Answer

omarelamrieado
omarelamrieado
18,473 Points

The __len__ method does not need the parameter item. Just remove it, and you should be fine.

Here's my solution:

class Liar(list):
    def __len__(self):
        actual_len = super().__len__()
        return actual_len + 1

Of course, you could just return a random number instead, but that's not the learning objective.

Here's my solution:

class Liar(list):
    def __len__(self):
        return 789