Learning Python now coming from Perl

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Dec 6 19:10:45 EST 2008


On Sat, 06 Dec 2008 14:15:28 -0500, Roy Smith wrote:

> In article <014a96e0$0$20670$c3e8da3 at news.astraweb.com>,
>  Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
> 
>> On Sat, 06 Dec 2008 08:50:20 -0500, Roy Smith wrote:
>> 
>> > For your first
>> > project, pick something that's small enough that you think you could
>> > tackle it in under 50 lines of Perl.
>> 
>> Is there anything which *can't* be written in under 50 lines of Perl?
> [...]
>> Also, Perl REs are faster than Python REs, or so I'm told. Between the
>> speed and the convenience, Perl programmers tend to use RE's for
>> everything they can. Python programmers tend to use REs only for
>> problems that *should* be solved with REs rather than *can* be solved
>> with a RE.
> 
> Well, as an old-time unix hacker (who learned REs long before Perl
> existed), my question to you would be, "Is there any problem which
> *shouldn't* be solved with an RE?" :-)


I think you've answered your own question:

> One of the reasons REs don't get used in Python as much as in Perl is
> because strings have useful methods like startswith(), endswith(), and
> split(), and also the "in" operator.  These combine to give you easy
> ways to do many things you might otherwise do with REs.


Also:

* splitting pathnames and file extensions

* dealing with arbitrarily nested parentheses

* any time you need a full-blown parser (e.g. parsing HTML or XML)

* sanitizing untrusted user input
  ("I bet I can think of *every* bad input and detect them all 
  with this regex!")

* validating email addresses
  http://northernplanets.blogspot.com/2007/03/how-not-to-validate-email-addresses.html

* testing prime numbers
  http://jtauber.com/blog/2007/03/18/python_primality_regex/

* doing maths
  http://blog.stevenlevithan.com/archives/algebra-with-regexes
  http://weblogs.asp.net/rosherove/archive/2004/11/08/253992.aspx



There's probably more.


-- 
Steven



More information about the Python-list mailing list