[Tutor] Please explain TypeError

D. Guandalino guandalino at gmail.com
Sun Aug 21 23:35:48 CEST 2011


Python documentation says:

> exception TypeError
>
> Raised when an operation or function is applied to an object of
> inappropriate type. The associated value is a string giving details
> about the type mismatch.

For example:

>>> 'foo' + (1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'tuple' objects

So far so good. But why the following code gives a TypeError too?

>>> 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?

Thanks.


More information about the Tutor mailing list