OK to memoize re objects?

Robert Kern robert.kern at gmail.com
Sat Sep 19 20:24:35 EDT 2009


kj wrote:
> 
> 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.

Just use re.search(), etc. They already memoize the compiled regex objects.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list