error messages raised by python got me thinking.

Guido van Rossum guido at python.org
Sun Oct 21 23:49:36 EDT 2001


Laura Creighton <lac at strakt.com> writes:

> Here is a common error:
> >>> f='1'  
> >>> g=2
> >>> f+g
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: cannot add type "int" to string
> --------------
> Here is another one:
> >>> g+f
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: unsupported operand types for +
> -----------
> 
> These are rather different error messages.
> 
> The reason I noticed this, is because every time I get the first
> error, my initial parsing is always: f is the int, and g is the
> string. I seem to expect order of the arguments as presented in the
> error message to be the order in which I typed them.  I think I would
> prefer 'Cannot add type string _and_ int'.  For me, adding is not something
> that I do _to_ an existing thing, but something you do from top to bottom
> (or in this case left to right).   Not quite the difference between a
> transitive and an intransitive verb, but it has that flavour.  Perhaps 
> I do not _add_ but _sum_.  [Both are transitive, but I sum the whole
> set, not an individual element of the set.  In my head anyway.]
> 
> This has possible implications in how easy it is to learn python.  Am
> I simply odd, or typical?  In an international world, what does
> 'typical' mean given than many languages don't have verb constructs
> like this at all?
> 
> (Further question: why the double quotes around _int_ but not _string_?)

That's a lot of questions!

The path through the code is rather different for the two cases: for
string+int, concatenation to a string is attempted, and this is what
issues the detailed but confusing first message.  But for int+string,
concatenation is not a possibility, since concatenation operation
doesn't have the fancy behavior that regular add has (which tries to
give the right operand an opportunity to provide an implementation if
the left operand doesn't know how to do it).

I can easily make the error for string concatenation better, e.g. by
changing it to

    "cannot concatenate 'string' and '%s'"

where %s is replaced by the type of the right argument.  (In cases
like this, I prefer to use single quotes around the type names, but it
sounds like the person who made this particular error message less
confusing didn't know of that preference.)

But in general, we can't always produce the error message you'd like
to see: e.g. if you try to add a Unicode string and a number, you get
the even less clear

    TypeError: coercing to Unicode: need string or buffer, int found

This one is hard to fix, due to the complicated logic attempted in the
Unicode case (the code that issues the error message has no idea that
it is part of a '+' operator).

I could also try to make the other error ("unsupported operand types
for +") clearer, e.g. by adding the actual operand types to the error
message, e.g.

    "unsupported operand type for +: 'int' and 'str'"

(It would have to be 'str' because as of 2.2, that's the name for the
string object type, corresponding to the function most commonly used
to create strings.)

Another way to write this, using 2.2 lingo, might be

    "unsupported operand types: int() + str()"

but this doesn't look very newbie-friendly to me!

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list