Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Frustration

Carlos Marin
Carlos Marin
7,997 Points

Subclassing a list

"Now I want you to 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.:"

I am getting a "Maximum recursion depth exceeded" when I try to get the length. What does this mean????

frustration.py
class Liar(list):
    def __init__(self, *args, **kwargs):
        self = super().__init__(*args, **kwargs)
        return self

    def __len__(self):
        return len(self)+1

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,750 Points

When the code len(some_obj) is encountered, it calls some_obj.__len__() if this method uses len(some_obj) the method will continuously call itself (recursion) until the maximum level (or depth) is reached.

By using super().__len__() you can get a true length then alter it in the Liar class.

Post back if you need more help. Good luck!!!

Carlos Marin
Carlos Marin
7,997 Points

Hey Chris! I was confused, it took me a while but It's starting to add up now. I just solved the code challenge named "frustration".

I am a bit puzzled with the current class object I have now. why is my list being cleared?

:: below is my code ::

class Liar(list):
    def __init__(self, *args, **kwargs):
         self = list.__init__(*args, **kwargs)
         return self

    def __len__(self):
        return super().__len__()+1

input: books = ['cooking', 'fishing']
input: len(Liar(books))
output: 1
input: Liar(books)
output: []
input: len(books)
output: 0

Thanks a ton! You're the best!
- Carlos A. Marin

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 67,750 Points

Interesting question. I didn’t notice it before. The answer may surprise you: the list created by Liar will always be empty

  • the method __init__ is not expected to return anything. It only modifies The self that points to the instance
  • the statement list.__init__(anything) will always return None
  • Assignment to variable that has the same name as a parameter creates new variable local to that method instead of changing the original object referenced by the parameter. By assigning self, the value self becomes local to the method.

Since nothing special is being performed by Liar.__init__ it could be removed:

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

books = ['cooking', 'fishing']
print(books)
print(len(Liar(books)))
print(Liar(books))
print(books)
print(len(books))
print(books)

#output
[cooking, fishing]
3
[cooking, fishing]
[cooking, fishing]
2
[cooking, fishing]
Carlos Marin
Carlos Marin
7,997 Points

Thanks for coming thru Chris! I feel loads better now! :D