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)
Frank Genova
Frank Genova
Python Web Development Techdegree Student 12,700 Points

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
Henrik Christensen
Python Web Development Techdegree Student 38,294 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
Henrik Christensen
Python Web Development Techdegree Student 38,294 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 :-)