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

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
231,236 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 68,441 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