accumulator generators

Cameron cameronlarue at gmail.com
Fri May 30 15:50:43 EDT 2008


I was reading this <a href="this http://www.paulgraham.com/icad.html">Paul
Graham article</a> and he builds an accumuator generator function in
the appendix. His looks like this:

<pre>
def foo(n):
  s = [n]
  def bar(i):
    s[0] += i
    return s[0]
  return bar
</pre>

Why does that work, but not this:

<pre>
def foo(n):
  s = n
  def bar(i):
    s += i
    return s
  return bar
</pre>



More information about the Python-list mailing list