[Python-ideas] re.compile_lazy - on first use compiled regexes

Gregory P. Smith greg at krypto.org
Fri Mar 22 23:42:49 CET 2013

On Fri, Mar 22, 2013 at 3:31 PM, Ronny Pfannschmidt <
Ronny.Pfannschmidt at gmx.de> wrote:

> Hi,
> while reviewing urllib.parse i noticed a pretty ugly pattern
> many functions had an attached global and in their own code they would
> compile an regex on first use and assign it to that global
> its clear that compiling a regex is expensive, so having them be compiled
> later at first use would be of some benefit

It isn't expensive to do, it is expensive to do repeatedly for no reason.
 Thus the use of compiled regexes.  Code like this would be better off
refactored to reference a precompiled global rather than conditionally
check if it needs compiling every time it is called.


> but instead of all that reptetive code there should be an alternative to
> re.compile that waits with compilation for the first use
> -- Ronny
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130322/b9eab55e/attachment.html>

More information about the Python-ideas mailing list