title: Can I have anonymous function with "static" variables in Python?
body: <p>Can I have anonymous function with "static" variables in Python?</p>
  
  <p>For example</p>
  
  <pre><code>lambda x: re.compile(r'foobar').match(x)
  </code></pre>
  
  <p>is not so great, because it may recompile every time it is called (if <code>re</code> runs out of cache - thank you responders for pointing out the cache mechanism).</p>
  
  <p>I can do this without recompiling:</p>
  
  <pre><code>def f(line):
      try:
          f.c
      except:
          f.c = re.compile(r'foobar')
      return f.c.match(line)
  </code></pre>
  
  <p>How to do it with a lambda, without recompiling?</p>
  
  <p>And well, I don't want to use a helper function, to use inside the lambda.  The whole point of using lambdas is "anonymity".  So yes the lambda is anonymous, and self-contained.</p>