Why are String Formatted Queries Considered So Magical?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Wed Jun 30 16:30:38 EDT 2010
On Wed, 30 Jun 2010 14:14:38 +0000, Jorgen Grahn wrote:
> On Tue, 2010-06-29, Stephen Hansen wrote:
>> On 6/29/10 5:41 AM, Roy Smith wrote:
>>> Nobody<nobody at nowhere.com> wrote:
>>>
>>>>> And what about regular expressions?
>>>>
>>>> What about them? As the saying goes:
>>>>
>>>> Some people, when confronted with a problem, think "I know, I'll
>>>> use regular expressions." Now they have two problems.
>>>
>>> That's silly. RE is a good tool. Like all good tools, it is the
>>> right tool for some jobs and the wrong tool for others.
>>
>> There's nothing silly about it.
>>
>> It is an exaggeration though: but it does represent a good thing to
>> keep in mind.
>
> Not an exaggeration: it's an absolute. It literally says that any time
> you try to solve a problem with a regex, (A) it won't solve the problem
> and (B) it will in itself become a problem. And it doesn't tell you
> why: you're supposed to accept or reject this without thinking.
It's a *two sentence* summary, not a reasoned and nuanced essay on the
pros and cons for REs.
Sheesh, I can just imagine you as a child, arguing with your teacher on
being told not to run with scissors -- "but teacher, there may be
circumstances where running with scissors is the right thing to do, you
are guilty of over-simplifying a complex topic into a single simplified
sound-byte, instead of providing a detailed, rich heuristic for analysing
each and every situation in full before making the decision whether or
not to run with scissors".
If you look at the quote carefully, instead of making a knee-jerk
reaction, you will see that it is *literally* correct. Given some
problem, having decided to solve it with a regex, you DO have two
problems:
(1) Merely making the decision "use REs" doesn't actually solve the
original problem, any more than "use a hammer" solves the problem of "how
do I build a table?". You've decided on an approach and a tool, but your
original problem still applies.
(2) AND you now have the additional problem of dealing with regular
expressions, which are notoriously hard to write, harder to debug,
difficult to maintain, often slow, incapable of solving certain common
problems (such as parsing nested parentheses).
So it might be a short, simplified quip, but it *is* literally correct.
> How can that be a good thing to keep in mind?
Because many people consider REs to be some sort of panacea for solving
every text-based problem, and it's a good thing to open their eyes.
--
Steven
More information about the Python-list
mailing list