[Tutor] Please explain TypeError

Walter Prins wprins at gmail.com
Mon Aug 22 00:31:38 CEST 2011


On 21 August 2011 22:35, D. Guandalino <guandalino at gmail.com> 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?
>

The more obvious answer was given by David, but I'd like to also add that
the reason why Python complains in this instance with *TypeError* (as
opposed to say something like "ArgumentError" or "ValueError" imagining for
the moment such things existed) is becuase, looking function objects, fn(a)
is type-incompatible with fn(a,b).   Differently put, the argument list is
part of the type of a function, and the different argument lists makes the
expected function and the actual provided incompatible, hence Python raises
TypeError.  (Note this is somewhat speculative on my part so take it for
what it's worth.)

Regards

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110821/9c553d6d/attachment.html>


More information about the Tutor mailing list