Little problem with the "+" operator

Nick Bensema nickb at fnord.io.com
Wed May 2 12:14:20 EDT 2001


In article <mt1yscm7yu.fsf at astron.berkeley.edu>,
Johann Hibschman  <johann at physics.berkeley.edu> wrote:
>Joshua Marshall writes:
>
>>> This is infinitely more sane than:
>
>>> % perl -e 'print 500 + "cool"'
>>> 500
>
>> Comparing Python to Perl here doesn't seem very fair.  They're very
>> different languages.  In general, I'm surprised by the urgency with
>> which people often jump to compare them.
>
>Well, Perl's the only language that I know of that lets you add 500 to
>"cool" without an error.  Well, I suppose tcl might.  But it's still a
>fairly safe inference that the original poster was coming from Perl.

Perl, Awk, and Visual Basic both have string concatenation operators
that don't look like the addition operator.  Python, Ruby, and Commodore
64 BASIC have ones that do.  But the point is, this is not a discussion
that called for a cheap swing at Perl.

One could just as easily argue that the approach Python takes is better
than alternatives because the type mismatch error prevents the programmer
from using ambiguous code.  The Perl statement above suggests that it
used the approach of assuming integer math, since it implies that "cool"
is converted to 0.  Suppose, instead, you had the expression (500 +
"500").  Would you rather have 1000, or "500500"?


-- 
Nick Bensema <nickb at io.com>               ICQ#2135445 
==== ======= ============== http://www.io.com/~nickb/



More information about the Python-list mailing list