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

Greg Waitt
Greg Waitt
6,599 Points

Need help validating answer

The code passes the "quiz" but I am having trouble validating the code. The list is printed and not the "liar length". What am I doing wrong? Thanks for your help.

frustration.py
class Liar(list):
    def __len__(self):
        lie=super().__len__() +3
        return lie

test2 = Liar(['a','b','c','d'])
print(test2)
Daniel Nakonieczny
Daniel Nakonieczny
Courses Plus Student 54,657 Points

I was playing with this in the terminal, and got this to work using python3:

frustration.py
class Liar(list):
    def len(self):
        lie=len(self)+3
        return lie

test2 = Liar(['a','b','c','d'])
print(test2.len()) #returns 7

1 Answer

Cool you are trying to make this work in the actual python REPL.

print(something) gets python to print out that something, but won't show the length. You want print(len(something)) to print the length.

Greg Waitt
Greg Waitt
6,599 Points

Thanks. I see now that the function returns len to the class. I cannot get the function result from calling the class directly (which is a list).