[Python-ideas] New pattern-matching library
greg.ewing at canterbury.ac.nz
Tue Mar 1 23:31:29 CET 2011
Guido van Rossum wrote:
> It's been tried before without much success. I think it may have been
> a decade ago that Ka-Ping Yee created a pattern matching library that
> used function calls ... It didn't get much use.
That may largely be due to marketing issues. A potential
user would have to know that Ka-Ping's module existed, or
be sufficiently dissatisfied with the status quo to go
looking for something like it. Probably it has never even
occurred to many people familiar with REs from other contexts
that there might be another way.
Whereas if there were a set of constructor functions available
right at hand in the re module, prominently featured in the
examples and reference docs, I suspect they would be used
quite a lot. I know that *I* would use them all the time,
whereas I've never been motivated enough to pull in another
module to get this functionality.
Perhaps the best way to think of this is not as a complete
replacement for traditional RE syntax, but as a set of
convenience functions for building up REs out of smaller
REs. It's not entirely straightforward to do that correctly,
taking into account escaping, operator precedence, etc.,
so having some functions available for it makes a lot of
sense. They would make it much easier to write readable
code involving complicated REs. Since we're a community
of people who believe that "readability counts", there
shouldn't be any argument that this is a desirable goal.
> On the third hand, I could see this as an area where a pure
> library-based approach will always be doomed, and where a proposal to
> add new syntax would actually make sense.
I don't think new syntax is necessary -- functions are
quite adequate for the task. But they need to be available
right at your fingertips when you're working with REs.
Having to seek out and obtain a third party library is
too high a barrier to entry.
More information about the Python-ideas