how often does re.compile compile?

Greg Ewing greg.ewing at compaq.com
Mon Sep 6 18:07:44 EDT 1999


"A.M. Kuchling" wrote:
> 
> wade at lightlink.com <wade at lightlink.com> wrote:
> >If I say re.compile() inside the function
> >definition, does the pattern get compiled afresh every time the function
> >is called?

The re.compile call certainly gets executed on every call.

> There's a cache containing the most recent 20-odd
> patterns, so if you're using fewer patterns, the re.compile() will ...
> find the precompiled cached pattern

If you don't want to rely on that, you can use the standard
hack for creating local precomputed things:

def f(..., pat = re.compile(my_expr)):
  ...

Just make sure you don't accidentally call f with one
too many parameters :-)

Greg




More information about the Python-list mailing list