Python to Perl Conversions

William Tanksley wtanksle at dolphin.openprojects.net
Fri Aug 20 00:03:30 EDT 1999


On 19 Aug 1999 08:21:55 -0700, Tom Christiansen wrote:
>Per your collective requests, here is one of the documents
>with my collective observations from learning Python.

A very cool document.  Looks like my observations about Perl, except I
didn't write anything down (too stupid-lazy).

>This article was posted to the Perl newsgroup, but I have duplicated
>the posting here without crossposting to avoid flame wars.

Good idea.  Pity it didn't work, eh?  ;-)

>--tom

>x / y                           Same for floats, but Perl doesn't do
>                                integer division here usually.  Python
>                                things 3/4 is 0 (and false), but 3.0/4.0 is 0.75

Q: can Perl be made to do integer division here if you declare both
variables as integer?  I seem to recall something like that (but it's been
a while).

>                                strings aren't numbers, nor are they
>                                autoconverted.

This is important to Python.  And Perl, of course.

>float(x)                        not applicable

Except for variables declared integer, right?

>pow(x,y)                        x ** y

x**y is also valid Python syntax.

>x << n, x >> n                  same, although Perl works differently if
>                                you go over 31 bits:  Python keeps giving 
>                                you 0, whereas Perl wraps.  1<<34 is 0 in 
>                                python but 4 in perl.

Facinating.  Is the Perl << actually a rotate, or is that a special case
for n > 31?

>~x                              same-ish, but see |&^ above for details.
>                                In particular, you can't negate bitstrings
>                                in python.

I'm not sure what bitstrings are, but Python's longs can certainly be
negated, and I'm not sure what more you can ask for without asking for
Common Lisp.

(Here's an idea: take Common Lisp, rename its functions, write a new
reader macro, and call it "Python 2.0"!)

>Python Construct                Perl Equivalent
>===========================================================================

>                                Note that Python doesn't support hash
>                                slices, which in Perl are simple:
>                                    @hash{k1,k2,k3} = (v1, v2, v3)

That's a good point, although I'm not sure what Python can do about it --
our slice semantics talk about gaps, and hashes don't have gaps.  I
suspect we'd have to call it something other than slicing (this seems
pretty reasonable).

>hash(x)                         huh?  "returns the hash value of the object,
>                                if any"

The key which a hash table would use to store and look up the indicated
data.

>hex(n)                          sprintf("%#x", n)
>                                Perl's hex() function is the opposite.
>                                It converts 22 into 37.  Python's hex
>                                function converts 37 into 0x22.  Oh my!

Now this is a cool difference, because it highlights a difference between
the languages.  hex in Python converts from an integral number to a
string; in Perl it converts from a number which is treated as a string to
a number (which can be treated as a string).

Amusingly, applying hex repeatedly in Perl results in an ever-increasing
number.

I think Perl is more than a little oddball here.  I tried to model what
was hapenning, but I couldn't figure it out.  Ah, here we go, Perl does
the equivalent of converting the number into a string using radix 10, and
then reading that string into an integer using radix 16.

This is exactly what you'd want if you'd just read a hex number in which
didn't begin with "0x", so the read system misinterpreted it as a decimal.

Am I right?  I'm pretty puzzled here...

>reload(module)                  First delete from %INC, then require the
>                                module again, as in:
>                                    delete $INC{"Module.pm"}
>                                    require Module;
>                                Note that Python doesn't directly support
>                                nested modules as in
>                                    require Dir1::Dir2::Dir3::Module;
>                                except via a disgustingly named hackaround
>                                module.

True for 1.4, not true after that.

And "ni!" isn't disgusting -- what's disgusting is what the times have
come to, that ruffians can walk about saying "ni!" to innocent old ladies.

>str(x)                          I thought this was the same as repr().

Similar, identical in many cases.  str() is more verbose (prettier) in
some cases.

>tuple(seq)                      n/a.  This is a nasty hack to deal with the
>                                fact that (1,2,3) is different from [1,2,3].
>                                Hm... maybe it's like this:
>                                    $aref = [1,2,3]; 
>                                    @list = @$aref;

By "nasty hack" you mean "obvious consequence", of course.  Just like
$obj->{$name} is an obvious consequence of interpolation.

Sorry, I'm getting editorial -- but then you tend to be very much so.

>file.flush()                    Either FH->autoflush(1) if you're using the
>                                aliasing modules, or else the klunky but
>                                fast:
>                                    $old_fh = select(FH);
>                                    $| = 1;
>                                    select($old_fh);
>                                Note that this just sets autoflushing
>                                on each subsequent output on that handle.

Q: the Python version merely flushes the file, while the Perl version
looks like it's actually setting a permanent flag in the file to flush
frequently.  Is that correct?

Python, OTOH, doesn't seem to have a way to set a file's flushing
properties -- you have to explicitly flush whenever you need it.  True?

>sys.exit(status)                exit(status) and no import is bloody
>                                necessary just to leave the program.
>                                But that Python does this by raising
>                                a SystemExit exception.  Perl can use
>                                END{} handlers to catch these and do
>                                at-exit processing, and Python doesn't.
>                                You have to roll your own.

Python uses "finally:" and "except SystemExit:" blocks to do that.  Is
that what you mean?

I seem to recall END blocks in Perl, although I never used them...  Don't
they remain active forever, like C's atexit function?  How do you
deactivate them?

>_exit(n)                                POSIX::_exit(n)

In Python _exit skips the atexit stuff.  True in Perl?

-- 
-William "Billy" Tanksley




More information about the Python-list mailing list