title: Access lru_cache's cache
body: <p><strong>General problem</strong>: How can I access a variable in a function's closure? </p>
  
  <p><strong>Specific problem</strong>: How can I access the raw <code>cache</code> from a python function wrapped with <code>functools.lru_cache()</code>?</p>
  
  <p>If I memoize a function (example taken from the <a href="https://docs.python.org/3/library/functools.html#functools.lru_cache" rel="noreferrer">docs</a>)...</p>
  
  <pre><code>@lru_cache(maxsize=None)
  def fib(n):
      if n &lt; 2:
          return n
      return fib(n-1) + fib(n-2)
  
  &gt;&gt;&gt; [fib(n) for n in range(16)]
  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
  
  &gt;&gt;&gt; fib.cache_info()
  CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
  </code></pre>
  
  <p>Here is where <code>cache</code> is defined: <a href="https://github.com/python/cpython/blob/f0851910eb8e711bf8f22165cb0df33bb27b09d6/Lib/functools.py#L491" rel="noreferrer">https://github.com/python/cpython/blob/f0851910eb8e711bf8f22165cb0df33bb27b09d6/Lib/functools.py#L491</a></p>
  
  <p>When <code>fib()</code> is memoized, <code>lru_cache</code> adds a <code>cache_info()</code> and <code>cache_clear()</code> function to the wrapper.  <code>cache_clear()</code> has access to <code>cache</code> and I have access to <code>cache_clear()</code> so can I somehow use that to access <code>cache</code> directly?</p>