Python vs. Perl, which is better to learn?

brueckd at tbye.com brueckd at tbye.com
Wed May 1 11:10:10 EDT 2002


On Wed, 1 May 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.

IMO this is what makes the Python way a Good Thing - that tiny bit of 
extra effort required to use them (hopefully) makes a person stop and 
consider if re's really are the right tool. It's pretty common to have 
someone wondering how to reduce the complexity and/or improve the 
performance of their re and have the answer be "don't use an re" or "do 
more of the work outside the re".

Just yesterday a Java guy was asking me to help him speed up his re that 
found Wiki names from a list of tokens. The first optimization was to not 
even call the re engine unless the token began with a capital letter. The 
next optimization was to see if the rest of the string matched the 
lowercase version of the rest of the string, and if not, consider it a 
Wiki name, so in the end he didn't use re's at all (he had a slightly 
liberal definition of a Wiki name).

When using a regular expression is the right solution, they are extremely 
powerful and elegant, so I understand the temptation to use them all over 
the place (I'm suspicious when people tell me they're using regular 
expressions "all the time" - sometimes it's for valid reasons, but often 
it's not).

> 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 + '\)'

Just a side note, you can make this even more readable:

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

-Dave






More information about the Python-list mailing list