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.
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
class Liar(list): def __len__(self): wrong_len = super().__len__(self) * 1.5 return wrong_len
Steven Parker217,570 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