Python vs. Perl, which is better to learn?

Christopher Browne cbbrowne at acm.org
Wed May 1 01:02:07 EDT 2002


Centuries ago, Nostradamus foresaw when "Mark McEahern" <marklists at mceahern.com> would write:
> [Billy Ng]
>> I am also new to python.  I think python will be my replacement of perl.
>> However, if I need to do very heavy regular expression, I still
>> prefer perl.
>> I just don't feel comfortable with the python's re syntax.
>
> I've heard this sentiment expressed before.  I can count on my hands the
> number of times I've used Perl--all before I learned about Python--so I'm
> largely ignorant of Perl.
>
> 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.

If you're using regexes really a lot, that could easily be preferable.

Mind you, I have found that the "More Pythonic Way" of constructing
complex regexes by building components, like the following, to have
merit:

  digit = '[0-9]'
  date = '\(' + digit + digit + digit + digit + '/' + digit + digit + '/'
  date = date + digit + digit + '\)'

By building the regex out of something with named components, I don't
have the horridness of line noise like:
   if ($line =~ /.*\s+\d+.\d+\s+\d+.\d+\s+.\d+.\d+/) {

Ultimately, the answer is that both approaches have their own merits
and demerits.  The "Perl thing" tends to be shorter, while the "Python
thing" tends to be more readable.

What do you want to prefer?
-- 
(concatenate 'string "cbbrowne" "@acm.org")
http://www3.sympatico.ca/cbbrowne/emacs.html
"One disk to rule them all,  One disk to find  them. One disk to bring
them all and in the darkness grind them. In  the Land of Redmond where
the shadows lie." -- The Silicon Valley Tarot Henrique Holschuh



More information about the Python-list mailing list