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

subclass Liar problem

I really didn't get the concept of the subclass and the technique with which we override the inherent functionality of built in methods.

I understand that we can't customise the built in function len() and therefore we're using "dunder len()" to modify it for our purposes.

Now, why are we using super(). and what is the standard structure of the code that I should follow in every situation escapes me.

Many thanks Isabella

frustration.py
class Liar(list):
    def __len__(self):
        wrong_len = super().__len__(self) * 1.5
        return wrong_len
class Liar(list):
    def __len__(self):
        wrong_len = super().__len__(self)
        wrong_len = wrong_len * 2
        return wrong_len

I've tried this as well

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

1 Answer

Steven Parker
Steven Parker
231,268 Points

You have the right idea about the override, and the reason you use "super" is that the best way to be sure to have a wrong answer is to fetch the right one (from the parent class) and then modify it. But you have a few implementation issues:

  • when you call the parent method, you don't need to supply "self" as an argument (the system does that)
  • the length is expected to be an integer, you won't want to use a float value here
  • multiplying the value won't guarantee a wrong answer, the string might be empty