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 trialbdi
8,377 Pointsunsure 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
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
231,198 PointsYour 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
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 Pointsbdi, 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, andsuper()
.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 theself
of the current instance must be provided in the call.These are both show below:
Post back if you have more questions. Good luck!!!
bdi
8,377 Pointsbdi
8,377 PointsGreat, thank you