[Tutor] Please explain TypeError

Steven D'Aprano steve at pearwood.info
Mon Aug 22 01:35:07 CEST 2011


D. Guandalino wrote:

>>>> class C(object):
> ...     def __init__(self):
> ...             pass
> ...
>>>> C(1)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: __init__() takes exactly 1 argument (2 given)
> 
> I'm having hard times understanding why a TypeError is raised here.
> Could you explain?

Because it is an error involving a type (class). Type C doesn't take any 
arguments. You gave it an argument. Therefore, it's an error regarding a 
type, hence, a TypeError.

If you feel that this is a fairly dubious line of reasoning, I agree 
with you! I think that such failures would be best given their own 
dedicated exception for "wrong number of arguments". But they don't:


 >>> import math
 >>> math.sin(1.1, 2.2)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: sin() takes exactly 1 argument (2 given)


No types involved here, math.sin is just a function.

Oh well.




-- 
Steven



More information about the Tutor mailing list