Python vs. Perl, which is better to learn?

Alex Martelli aleax at aleax.it
Wed May 1 02:59:10 EDT 2002


Christopher Browne wrote:
        ...
>> Could you give us a concrete example where the use of regular expressions
>> in Perl is superior to Python?
> 
> Well, in Perl, they're "first class objects," as it were, requiring no
> extra references to functions to get them to function, as a base part
> of the language syntax.

I don't think this is the accepted and commonly used definition of "first 
class object".  http://www.cs.unm.edu/~crowley/phdExams/1997xfall/pl.html
for example:
"""
A first class object in a programming language is a language object that 
can be created dynamically, stored in a variable, passed as a parameter to 
a function and returned as a result by a function.
"""

It's true that Perl has several things "bundled" as an intrinsic, 
non-negotiable part of "the basic language" which Python places in separate 
modules.  For example, in Perl, @ARGV is an intrinsic global variable that 
holds the script's arguments, while in Python the same arguments are held
in sys.argv and thus to access them you need an 'import sys'.  I think it 
would be misleading to argue that this makes the arguments "first class" in
Perl and not in Python -- they're just as easy to access and use in either
case, it's just that, in Python, they're not willy-nilly injected into your 
top-level namespace when you don't care about them.

The situation is basically identical wrt regular expressions.  Ease of use 
and power are the same.  I've recoded lots of Perl scripts into Python, and
re's were the least of the translation difficulties (it seems to me the re
module is inspired by Perl's re's, so the similarity is very strong). 

Often in Python there may be _better ways_ to perform tasks for which a
Perl-used developer automatically reaches for re's, but that's another 
issue.  When you're transliterating you can still use re's, then if need be
you do a "peephole cleanup" pass to get rid of the dispensable ones:-).


Alex




More information about the Python-list mailing list