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

Jennifer Lithgow
Jennifer Lithgow
11,778 Points

Frustration.py

I am super lost with this challenge. I can follow along with the examples in the preceeding video, but I have no idea how I could alter len to give a wrong number. I don't even know where to begin on this one. In IDLE, tried to even just set it up and put in "pass" just to see if I could get len to work WITHOUT altering it, and I couldn't get anything to run. "Frustration" is an appropriate name for this challenge. :(

frustration.py
class Liar(list):
    def __len__(self):
        super().__len__()
        return self

10 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

If you call super().__len__() (and assign it to a variable, of course), you'll get the length of self (well, the instance, but that's the name inside the method), using __len__ from the parent class (list). Using that value, you can change it so that it's wrong. Add 1, remove 1, multiply by 2, whatever. Then return that new value.

Jennifer Lithgow
Jennifer Lithgow
11,778 Points

"and assign it to a variable, of course" ooooooohhhhh, okay. That seems so simple, now! I don't know why I wasn't able to wrap my head around that before. (Too many lectures in a row, maybe? Ha ha ha!) That's super helpful. I'll go back and give it another go. Thank you!

Could you elaborate a bit on when you would choose to set super().whatever (or just the magic method in the case of immutable types) as a variable and when you wouldn't?

ex:

class WeirdList(list):
    def __len__(self):
        super().__len__()

vs

class WeirdList(list):
    def __len__(self):
        weirdlen = super().__len__()
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Sharla Kew it all depends on what the method does.

class BaseClass:
    def set_name(self, name="Kenneth"):
        self.name = name

class ChildClass(BaseClass):
    def set_name(self):
        super().set_name()
        self.name = self.name.upper()

In this example, calling the super() version of the method changes the instance and doesn't return anything so it's still effective to use it.

With __len__, though, it doesn't set anything on the instance and it returns a value. That return value needs to go somewhere, so we put it into a variable.

Kenneth Love Ok so -

Some methods, what they do is set a variable, so why would you set a variable equal to the setting of a variable? ex: the set_name() method in your example.

Other methods, what they do is return a value, and you gotta store that in a variable or it vanishes into the ether. ex: __len__()

Putting super() on the beginning of them made them seem spooky but they need to be treated like normal methods.

Nice. Thanks Kenneth!

You can just return 2. That worked for me.

Qasa Lee
Qasa Lee
18,916 Points
class Liar(list):
    def __len__(self):
        return super().__len__() + 1

Check this out, good luck !

OK everybody here's the answer:

import random
import math

class Liar(list):
    def __init__(self, *arg):
        self.arg = []
    def __len__(self):
        super().__len__()
        randomNum = random.randint(2, 9)
        x = len(self.arg) + math.factorial(randomNum)
        return x

When you paste that in it will be correct. So mark the differences between yours and mine. Hopefully this helps.

Jay Norris
Jay Norris
14,824 Points

I'm still lost. Am I going in the wrong direction here?

def Liar(list):
    def __len__(self):
        length = super().__len__()
        lie = length * 2
        return lie
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You're really close. First, how do you define a class?

Second, it needs to always return a wrong length. What length would your current version give if there wasn't anything in the list?

Cristian Romero
Cristian Romero
11,911 Points

hmm even after adding class this still doesn't work?

Jay Norris
Jay Norris
14,824 Points

That worked! I was never going to notice that "def"

Thank you!

import random
from operator import add, sub


class Liar(list):
    def __len__(self):
        ops = (add, sub)
        op = random.choice(ops)
        length = super().__len__()

        return op(length, 3)

This post is pretty old, but I'm just gonna leave this here.

import random


class Liar(list):
    def __init__(self,*arg):
        self.arg = []
    def __len__(self):
        super().__len__()
        randomNum = random.randint(0,10)
        x = randomNum + len(self.arg)
        return x

import random import math

class Liar(list): def init(self, *arg): self.arg = [] def len(self): super().len() randomNum = random.randint(2, 9) x = len(self.arg) + math.factorial(randomNum) return x

sonny unverferth
seal-mask
.a{fill-rule:evenodd;}techdegree
sonny unverferth
Python Web Development Techdegree Student 2,889 Points

Hey Ken, I have followed the python course from the beginning without skipping anything. Im stuck on this Challenge and have never seen _ _ len _ _ or .super() before. Its really frustrating. Any advice?

Temiye Oluseun
Temiye Oluseun
2,902 Points

Hey try this class Liar(list): def len(self): length = super().len() wrong = 2 return wrong