Perl is worse!

Niklas Frykholm r2d2 at mao.acc.umu.se
Mon Jul 31 03:07:36 EDT 2000


>    It isn't senseless.  That is the whole point.  It is only senseless
>because of typing.  Clearly you cannot add a word to a number, granted.  But
>what of 1 + "1"?  That isn't senseless, those are two numbers.  I can see they
>are two numbers, it is only because of typing that it fails.  
>
>    So I ask you this /VERY/ simple question.  Why can't Python do both?  Hm?
>What is wrong with taking 1 + "1", converting the "1" to a 1 and adding it
>together?

I hate to step into a thread that is already so bloated, but I feel much of
the discussion is missing the point. There is nothing inherently wrong with
having 1 + "1" == 2. It's a design choice, and for Perl, it is a design
choice that makes sense.

However, for Python, I do not believe it would be a good design choice,
because it implies that operators are type specific. So you need separate
operators for adding numbers (+) and concatenating strings (.). This is
not that troublesome, because these are really different operations. More
disturbing is that we need separate operators for comparing numbers (==)
and comparing strings (eq).

In Perl, this is not such a big problem, because Perl doesn't care that
much about types. Often the types are implicit (the programer says to
himself, well I'll represent cars by a list with the model in the first
element and the year in the second - but doesn't actually define a Car
type). (Of course, with objects and references this changes, but then
automatic conversion breaks down as well \1 + 1 is not 2.)

In Python, on the other hand, the programmer creates new types (classes)
all the time, classes for rationals, matrixes, cars, etc --- things that we
want to add, multiply and compare. With type specific operators, we would
have to create new operators for each type, which I do not think is a very
good idea. It would make the code harder to read and write and it would
break the similarity with mathematic notation where + can indeed be used to
add many things beside numbers.

>    Hell, why do an exception at all?  Why not do what is already done with
>integers, reals and floats?  
>
>1 + 1 = math
>
>1 + "1" = math
>
>1 + "foo" = string ("1foo")
>
>"foo" + "foo" = string ("foofoo")

Because it breaks down associativity?

1 + (1 + "foo") = "11foo"

(1 + 1) + "foo" = "2foo"

// Niklas



More information about the Python-list mailing list