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

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
210,936 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