More random python observations from a perl programmer

Tom Christiansen tchrist at mox.perl.com
Thu Aug 19 21:32:15 CEST 1999


     [courtesy cc of this posting mailed to cited author]

In comp.lang.python, 
    duncan at rcp.co.uk (Duncan Booth) writes:
:People may enjoy this but personally I would just do
:    	num = int(sys.stdin.readline())
:BTW, I am not commenting on the comparisons with Perl since I don't claim 
:to know that language, but I seem to remember hearing that Perl would 
:silently convert the string "42x" into the number 42 without throwing an 
:error, now there is scary.

Sorry, I was doing to do just one or two, but your reply
merits more.

    % perl -le 'print int("42x")'
    42
    (Exit 0)

Add warnings:

    % perl -wle 'print int("42x")'
    Argument "42x" isn't numeric in int at -e line 1.
    42
    (Exit 0)

Promote numeric-class warnigns to exceptions:

    % perl -wle "use warning FATAL => 'numeric'; print int('42x')"
    Argument "42x" isn't numeric in int at -e line 1.
    (Exit 255)

In short, Perl doesn't normally hurl.  But you can ask it to, 
at least in newer, undocumented releases. :-)

    % perl -v
    This is perl, version 5.005_60 built for OpenBSD.i386-openbsd
    Copyright 1987-1998, Larry Wall

:Try reading the manual page doc/lib/re-syntax.html in your python 
:distribution.

    % man doc/lib/re-syntax.html
    man: no entry for doc/lib/re-syntax.html in the manual.
    (Exit 1)

Just kidding. :-)

:Or use the write method of the file which doesn't do any of the print 
:prettifying.

Good point.

:>    You can't just interchange tuples and lists in python the way
:>    you can lists and arrays in Perl.
:Or the way you can interchange strings and numbers in Perl.
:They are different types. (Why we need two list types is another question 
:though).

They're only two types if you think of them that way.  But they're not if
you don't. :-)  It's like saying chars and short ints and ints and long
ints and long long ints and floats and doubles (which are long floats)
and long doubles (which are long long floats) are different types.
Or you can just call them numbers.

Now, complex, *those* are different. :-)

:>GOTCHA: (low)
:>    You have to compiled the definition for a  function before you may
:>    compile the call it.
:Not true, you have to compile the definition for a function before you may 
:execute the call to it. You may compile the call before the function if you 
:wish.

Python deems def an executable statement.  Perl doesn't.  Therefore,
it's a gotcha for a Perl programmer, who doesn't understand why the
silly compiler did that.  Apparently, it's just at run-time interring
a function that was compiled at compile-time in the current symbol table.
I've done that in Perl, expecially for local functions, but people
glare at me when I do. :-)

:>    ADDENDA: It turns out that def() happens *AT RUN TIME* in python.
:>    That's the root of this problem.  In Perl, a sub{} is a compile-time
:>    thing.
:Dont forget that in Python you can stick your def in a for loop to define a 
:whole host of functions with different default arguments.

Ah, like this, using lexical closures (you're expected to "ooh" and
"aah" now :-) and direct symbol table diddling:

    my @colors = ('red', 'blue', 'green', 'yellow', 'orange', 'purple');
    for my $name (@colors) {
        *$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
    } 

That's what gets me talked about. :-)

BTW, the @colors assignment is more legibly written using the qw//
syntactic sugar:

    @colors = qw(red blue green yellow orange purple);

--tom
-- 
Perl itself is usually pretty good about telling you what you shouldn't do. :-)
        --Larry Wall in <11091 at jpl-devvax.JPL.NASA.GOV>




More information about the Python-list mailing list