how to avoid leading white spaces

rurpy at yahoo.com rurpy at yahoo.com
Fri Jun 3 08:51:18 EDT 2011


On 06/02/2011 07:21 AM, Neil Cerutti wrote:
> > On 2011-06-01, rurpy at yahoo.com <rurpy at yahoo.com> wrote:
>> >> For some odd reason (perhaps because they are used a lot in
>> >> Perl), this groups seems to have a great aversion to regular
>> >> expressions. Too bad because this is a typical problem where
>> >> their use is the best solution.
> >
> > Python's str methods, when they're sufficent, are usually more
> > efficient.

Unfortunately, except for the very simplest cases, they are often
not sufficient.  I often find myself changing, for example, a
startwith() to a RE when I realize that the input can contain mixed
case or that I have to treat commas as well as spaces as delimiters.
After doing this a number of times, one starts to use an RE right
from the get go unless one is VERY sure that there will be no
requirements creep.

And to regurgitate the mantra frequently used to defend Python when
it is criticized for being slow, the real question should be, are
REs fast enough?  The answer almost always is yes.

> > Perl integrated regular expressions, while Python relegated them
> > to a library.

Which means that one needs an one extra "import re" line that is
not required in Perl.

Since RE strings are complied and cached, one often need not compile
them explicitly.  Using match results is often requires more lines
than in Perl:
   m = re.match (...)
   if m: do something with m
rather than Perl's
   if m/.../ {do something with capture group globals}
Any true Python fan should not find this a problem, the stock
response being, "what's the matter, your Enter key broken?"

> > There are thus a large class of problems that are best solve with
> > regular expressions in Perl, but str methods in Python.

Guess that depends on what one's definition of "large" is.

There are a few simple things, admittedly common, that Python
provides functions for that Perl uses REs for: replace(), for
example.  But so what?  I don't know if Perl does it or not but
there is no reason why functions called with string arguments or
REs with no "magic" characters can't be optimized to something
about as efficient as a corresponding Python function.  Such uses
are likely to be naively counted as "using an RE in Perl".

I would agree though that the selection of string manipulation
functions in Perl are not as nice or orthogonal as in Python, and
that this contributes to a tendency to use REs in Perl when one
doesn't need to.  But that is a programmer tradeoff (as in Python)
between fast-coding/slow-execution and slow-coding/fast-execution.
I for one would use Perl's index() and substr() to identify and
manipulate fixed patterns when performance was an issue.
One runs into the same tradeoff in Python pretty quickly too
so I'm not sure I'd call that space between the two languages
"large".

The other tradeoff, applying both to Perl and Python is with
maintenance.  As mentioned above, even when today's requirements
can be solved with some code involving several string functions,
indexes, and conditionals, when those requirements change, it is
usually a lot harder to modify that code than a RE.

In short, although your observations are true to some extent, they
are not sufficient to justify the anti-RE attitude often seen here.




More information about the Python-list mailing list