OK to memoize re objects?

kj no.email at please.post
Sun Sep 20 01:22:12 CEST 2009



My Python code is filled with assignments of regexp objects to
globals variables at the top level; e.g.:

_spam_re = re.compile('^(?:ham|eggs)$', re.I)

Don't like it.  My Perl-pickled brain wishes that re.compile was
a memoizing method, so that I could use it anywhere, even inside
tight loops, without ever having to worry about the overhead of
regexp compilation.

Of course, I can do the memoization myself.  Would it be a bad
idea?  How much state does a re object keep? Or to put it differently,
what should be avoided to keep a regexp object essentially "stateless",
so that its memoization makes sense?

TIA!

kynn



More information about the Python-list mailing list