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

Nick Coghlan ncoghlan at gmail.com
Sat Mar 23 03:00:12 CET 2013


On Fri, Mar 22, 2013 at 3:42 PM, Gregory P. Smith <greg at krypto.org> wrote:
>
> 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.

Alternatively, if there are a lot of different regexes, it may be
better to rely on the implicit cache inside the re module.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list