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

unsure why im getting this error: TypeError: descriptor '__init__' of 'list' object needs an argument

class Liar(list):

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

    def __len__(self):
        liar_len = super().__len__()+2
        return liar_len
frustration.py
class Liar(list):

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

    def __len__(self):
        liar_len = super().__len__()+2
        return liar_len

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

1 Answer

Steven Parker
Steven Parker
210,424 Points

Your explicit call to list.__init__ is missing the self argument. But you don't need to override __init__ at all here, simply redefine __len__:

class Liar(list):
    def __len__(self):
        liar_len = super().__len__() + 2
        return liar_len
Chris Freeman
Chris Freeman
Treehouse Moderator 67,636 Points

bdi, as an aside to Steve’s answer, there are two ways to call the base class’s __init__ method, if you really needed to. This explanation might be too advanced for now. Bookmark and come back later after covering inheritance, class methods, and super().

The preferred method is to use super(). It understands accessing the parent classes and calls a bound version (self predefined) of the method of the parent class (this is why “self” is not needed).

This other way is to call the __init__ method of the parent class directly. This is viewed as less “Pythonic”. When calling this class method, the method is “unbound” to an instance and has no value for “self”, so the self of the current instance must be provided in the call.

These are both show below:

        super().__init__(*args, **kwargs)
        # OR
        list.__init__(self, *args, **kwargs)

Post back if you have more questions. Good luck!!!

Great, thank you