Python to Perl Conversions

Gordon McMillan gmcm at hypernet.com
Fri Aug 20 01:27:48 EDT 1999


Couple comments -

Tom Christiansen wrote:
>                                 Yes, slices in python don't include
>                                 the termination point.

This might be a surprise if you're not used to it, but it turns out 
to be very nice. Basically any way of expressing a range will result 
in something that includes the left hand side, but excludes the right 
hand. So when you're slicing, chopping up, inserting... you (almost) 
never have to use "i-1".

> delattr(object,name)            n/a?  "delete named attribute from
>                                 object".
>                                 I suppose this might be
>                                         delete $obj->{name}
>                                 but that's pretty uncool to do it
>                                 without asking the object.

I'm not sure I've *ever* seen this used in Python. But it hints at a 
very nice piece of syntactic sugaring you can do.

 class A:
    pass

 a = A()
 a.name = "Joe"
 a.shoesize = 13

All I'm doing is manipulating a dict (a's __dict__), but in a way 
that (sometimes) yields much more readable code than:
 a = {}
 a["name"] = "Joe"
 a["shoesize"] = 13


> 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.

Are you referring to "ni"? Again, this is something that has changed. 
Although Python packages may well have their own "gotchas" for Perl 
folks.
 
> str(x)                          I thought this was the same as
>                                 repr().

It often is, and defaults to repr. But if they are differnt, str
means "as something a user should see" and repr means "as something
a programmer should see" (and, if at all possible, something that
eval will do the inverse of).

> 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;

When you get used to them, you find that you use lists and tuples 
completely differently. The first thing you find is that because 
tuples are immutable, they can be used as keys in dicts.  But 
more to the point, lists are used as collections, while tuples are 
used as structs or instances of simplistic classes might be. So you 
soon think of them as completely different beasts, even if they are 
syntactically similar.

> type(obj)                       ref($obj) returns its string type

Nit: In Python, "type" returns a type object.
 
> 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.

This is not the normal way to leave a Python program, either. The
normal way is just to fall out. Also note that an import of a
built-in costs (almost) nothing.  Namespaces are kept as clean as
possible - maybe because you can clobber any name except a keyword.

> sys.setcheckinterval(reps)      n/a.  This is some event hook
>                                 processing
>                                 thing.

Actually, it's how may byte code instructions to do in one 
"time-slice" if you're using threads.

> string.splitfields(s,sep)       @a = split(/sep/, $s)   # no import
>                                 If you don't want regex magic chars:
>                                 @a = split(/\Qsep/, $s) 

Deprecated. Use the (newer) optional args to split().

> string.joinfields(x, sep)       $s = join($sep, @x);    # no import

Deprecated. Use optional args to join().

> posix.environ                           The %ENV hash; no import
>                                         needed in Perl.  However, assignment doesn't 
>                                         appear to propagate to
>                                         unborn children as it does in Perl.

Use putenv() to do that.


- Gordon




More information about the Python-list mailing list