title: Conditional memoization?
body: <p>There are plenty of memoization decorators out there, but I'm curious how one would write a memoization decorator which supports arbitrary function signatures, but lets the function decide when to memoize a result? Something like this:</p>
  
  <pre><code>def conditional_memoize(f):
      cache = {}
      @wraps(f)
      def conditional_f(*args, **kwargs):
          return f(*args, **kwargs)
      return conditional_f
  
  @conditional_memoize
  def my_func(a, b, c):
      if str(a) + str(b) + str(c) in cache:
          return cache[str(a) + str(b) + str(c)]
      res = # compute the result
      if some_arbitrary_condition:
          cache[str(a) + str(b) + str(c)] = res
      return res
  </code></pre>
  
  <p>However, I know this won't work because of the <code>NameError</code>. Is there a clever approach to the problem anyway? I could always use a class method and a class cache, just wanted to see if there was a decorator pattern for this.</p>