Python Object-Oriented Python Advanced Objects Frustration

Guy Larkin
Guy Larkin
1,662 Points

Bad question: wtf? This is my fourth solution to this problem, and all I get is Bummer: Try again!

As stated above, I have tried several different things, and I just keep getting "Try again!" I am flailing. I think I really don't understand classes and overriding methods. Can I at least get a clue about where to start attacking this problem? Thank you.

frustration.py
class Liar(list):
    super()
    def __new__(*args, **kwargs):
        len = len.__new__(*args, **kwargs)
        return len(list) * 3 / 2 + 2

3 Answers

Cheo R
Cheo R
37,145 Points

Looks like your first method is off by one space (see how the two def don't line up). Other than that it should be passing (you could also do the import random part at before the class definition).

Guy Larkin
Guy Larkin
1,662 Points

Thank you! Fixing the space didn't pass, but moving import random outside the class definition did.

Cheo R
Cheo R
37,145 Points

You might be over thinking it (which is common). Sometimes we just need to step back, take a break and review what we understand of the problem.

When I don't think I understand the problem, I like to take the prompt and make it into comments. Adding code as I go along. For example:

# make a subclass of list
# Name it Liar.

# Override the __len__ method
# so that it always returns the wrong number of items in the list.

# For example, if a list has 5 members, the Liar class might say it has 8 or 2.

# You'll probably need super() for this.

Then fill in what you know:

# make a subclass of list
# Name it Liar.
class Liar(list):

# Override the __len__ method

# so that it always returns the wrong number of items in the list.
return 1000  # or import random and use it to return a random number

# For example, if a list has 5 members, the Liar class might say it has 8 or 2.

# You'll probably need super() for this.

Then investigate about the parts you don't know.

# make a subclass of list
# Name it Liar.
class Liar(list):

# Override the __len__ method

def __len__(self):
    return random number here

# so that it always returns the wrong number of items in the list.
return 1000  # or import random and use it to return a random number

# For example, if a list has 5 members, the Liar class might say it has 8 or 2.

# You'll probably need super() for this.
# tried using super, tried without super. it works.
Guy Larkin
Guy Larkin
1,662 Points

I still can't get it to work. Have tried multiple new iterations, and reviewed the video lectures 4 or 5 times. Here is what I have now:

Still have no solution. Here is my latest code, after another set of many revisions and multiple reviews of the lectures: ''' class Liar(list): import random

    def __init__(self, *args, **kwargs):
        super().__init__()

   def __len__(self, *args, **kwargs):
        l = random.randint(1, 100) // 3 + 1
        return l

'''

Guy Larkin
Guy Larkin
1,662 Points

Thank you. This is a clear approach to breaking down the problem. It is similar to how I approach it, but it is good to write it down like this rather than hold it in my head.