According to wikipedia, 'a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be "closed over" its free variables.' An example should make it clearer.
adder
is a so-called factory function. We exercise it like so:So the question on SO was, "why does the nested function" (func, assigned to f) "remember the first value" of n?
One answer is: because that's the rule!
f.func_closure
is a tuple. The first item is a "cell" that contains the same int
object passed to adder
in constructing f
.More about function objects here