I find myself writing code like this relatively often:
_munge_text_re = re.compile("... complicated regex ...")
def munge_text(text):
match = _munge_text_re.match(text)
... do stuff with match ...
Only munge_text
uses _munge_text_re
, so it would be better to make it local to the function somehow, but if I move the re.compile
line inside the def
then it will be evaluated every time the function is called, defeating the purpose of compiling the regular expression.
Is there a way to make _munge_text_re
local to munge_text
while still evaluating its initializer only once? The single evaluation needn't happen at module load time; on the first invocation of munge_text
would be good enough.
The example uses a regex, and the majority of the time I need this it's for a regex, but it could be any piece of data that's expensive to instantiate (so you don't wanna do it every time the function is called) and fixed for the lifetime of the program. ConfigParser
instances also come to mind.
Extra credit: For reasons too tedious to get into here, my current project requires extreme backward compatibility, so a solution that works in Python 2.0 would be better than one that doesn't.