Perl is worse!

Paul Boddie paulb at infercor.no
Fri Jul 28 04:42:15 EDT 2000


Steve Lamb wrote:
> 
> On Fri, 28 Jul 2000 01:24:08 -0400, Tim Peters <tim_one at email.msn.com> wrote:
> >Steve, in case it isn't clear yet, Python programmers *want* to be blown out
> >of the water when doing something as senseless as
> 
> >    1 + "foo"
> 
>     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.

Having seen Perl's behaviour in cases like the one Tim quoted, albeit in the
following form...

  1 + $variable

(trying to remember Perl syntax) where $variable is some text which should have
been a number but for various reasons happened to be something like "foo", I
would rather my chosen implementation language did raise an exception than
pretend nothing was wrong.

>     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?  If it is a string that cannot be converted to an integer, throw an
> exception, otherwise, do it.  Then that preserves the 1 + "foo" exception and
> also does the sane thing of getting rid of types when it makes sense to do so.

What about 1 + "23"? Is "23" a decimal number or a hexadecimal number or what?

>     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")

Doesn't Perl do this last one as well? I refer you to the example I give above -
to have such behaviour permeating one's code is "nasty" to say the least.

> "foo" + "foo" = string ("foofoo")
> 
>     You already accept:
> 
> 1 + 1 = integer
> 
> 1 + 1.2 = float
> 
> 1 + 1j = complex
> 
>     So please don't tell me you don't want "automagic" type changing when it
> is already there and, I'd wager, you use it extensively.

Actually, this kind of thing does irritate some people, but at least one can
claim that such behaviour for numbers is more or less accepted in wider circles,
whereas 1 + "foo" is meaningless to almost everyone except Perl coders.

Regards,

Paul



More information about the Python-list mailing list