title: Memoization python function
body: <p>Here's a little piece of code which converts every function to its memoization version.</p>
  <pre><code>def memoize(f): # Memoize a given function f
      def memf(*x):
          if x not in memf.cache:
              memf.cache[x] = f(*x)
          return memf.cache[x]
  
      memf.cache = {}
      return memf
  </code></pre>
  <p>For instance, if we have a function <code>fib</code> as follows which returns the <code>n</code>th Fibonacci number:</p>
  <pre><code>def fib(n):
      if n &lt; 2:
          return 1
      else:
          return fib(n-1) + fib(n-2)
  </code></pre>
  <p>Now, the above function can be memoized by using</p>
  <pre><code>fib = memoize(fib)
  </code></pre>
  <p>Everything is fine up to this point but what I can't understand is that if we do something like this, instead of:</p>
  <pre><code>fib = memoize(fib)
  </code></pre>
  <p>we instead do:</p>
  <pre><code>fib2 = memoize(fib)
  </code></pre>
  <p>the function <code>fib2</code> isn't a <code>memoize</code>d function of <code>fib</code>. When we run <code>fib2</code> it runs like ordinary fib. Please explain why this <code>memoize</code> function gets applied to say a function <code>f</code> if and only if we use:</p>
  <pre><code>f = memoize(f)
  </code></pre>
  <p>The memoization code is taken from 6.00x a MOOC provided by edx.org. It's not running right now that's why I have come here to ask.</p>