Python Object-Oriented Python Advanced Objects Frustration

Matt Otis
Matt Otis
1,400 Points

Frustration.py and Super() Value Mart

Seems like this code should work....

frustration.py
class Liar(list):
    def __len__(self):
        actual_len = super().__len__(self)
        dummy_len = 5
        if actual_len == dummy_len:
            dummy_len += 1
        return dummy_len

But, I’m getting return of...

TypeError: expected 0 arguments, got 1

Why would it expect 0 arguments when I’m clearly trying to call a method and get a value?

Also, where am I going wrong?

2 Answers

Anthony Crespo
Anthony Crespo
Python Web Development Techdegree Student 12,958 Points

The function __ len__ actually don't take any argument.

If you take the argument off your code it will work.

class Liar(list):
    def __len__(self):
        actual_len = super().__len__(). # don't need to pass anything here
        dummy_len = 5
        if actual_len == dummy_len:
            dummy_len += 1
        return dummy_len

I just started learning python but this is how I understand it.

Normally, when you want to call the len() function in your code you will do this:

a = [1,2,3]
len(a)

It tells to the variable "a" to call the function __ len__ and it will return the length of it. It mean you could do:

a = [1,2,3]
a.__len__()

Like you can see len() who is __ len__() don't need a variable it already knows you want to return the length of "self".

class Liar(list):
    def __len__(self):  # this function override the __len__ function of list...
        actual_len = super().__len__(). # ...but you can still use it if you want and get the length like before you override it
        dummy_len = 5
        if actual_len == dummy_len:
            dummy_len += 1
        return dummy_len

Use self.function_name to call a function in your class and super().function_name to call one of the function your class inherited.

Matt Otis
Matt Otis
1,400 Points

Thanks for your insight. I hadn’t quite put together that:

len(a) == a.__len__()

That and your insight about self.function_name and super().function_name was super helpful in helping me ‘get’ it.

Thanks Anthony!

fwiw this is the code that passed:

frustration.py
class Liar(list):
    def __len__(self):
        actual_len = super().__len__()
        dummy_len = 5
        if actual_len == dummy_len:
            dummy_len += 1
        return dummy_len
Anthony Crespo
Anthony Crespo
Python Web Development Techdegree Student 12,958 Points

No problem I’m glad I could help! I had the same question when I did that part of the course.