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,126 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,428 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