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

IKE EGBuns
IKE EGBuns
3,848 Points

frustration.py

Pls can someone help. I am stuck at the moment and dont knw what I am doing wrong

frustration.py
import random


class Liar(list):
    def __init__(self, item):
        super().__init__()
        self.item = item

    def __len__(self):
        x = list.__len__(self.item)
        print(x)
        return random.randint(x, 9)

Hi IKE, I had a hard time with this one. I figured it out by looking at some other posts. It will pass if you provide only the code to override len method. So I would start with that. I think that super() gets used inside of your len(self) because it is what you want to override. It helps if you put the code into a new workspace (rather than the challenge window) or use an IDE to see what you are getting by adding print() to show steps along the way.

1 Answer

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Looks like you're trying to do more than you're asked to :-)

class Liar(list):
    def __len__(self):
        return super().__len__() + 2

You could replace the 2 with the randint() :-)

IKE EGBuns
IKE EGBuns
3,848 Points

Please Henrik, I noticed that you do not have the instance 'self' in the " len" method. Pls why is that so. I am also really struggling to finding a good documentation for the Oop and Magic method. Thanks

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

I did put self as an argument inside len like this: super().len(self) + 2 but when I did that, I got an error saying "len() expected 0 arguments but it found 1" so I removed self from len() and voila, it passed :-)

I can't remember everything, so when using something I rarely use (like len) I keep trying with different combinations + reading docs (i.e. python.org) until it works as I want it to :-)