Why do closures do this?
t at jollybox.de
Sun Aug 28 16:04:56 CEST 2011
On 28/08/11 05:45, John O'Hagan wrote:
> Somewhat apropos of the recent "function principle" thread, I was recently surprised by this:
> for n in range(3):
> def f():
> return n
> [i() for i in funcs]
> The last expression, IMO surprisingly, is [2,2,2], not [0,1,2]. Google tells me I'm not the only one surprised, but explains that it's because "n" in the function "f" refers to whatever "n" is currently bound to, not what it was bound to at definition time (if I've got that right), and that there are at least two ways around it: either make a factory function:
This does not do what you'd like it to do. But let's assume that, it
did, that Python, when encountering a function definition inside a
function, "froze" the values of nonlocal variables used in the new
function, from the point of view of that function — that *might* be more
intuitive, at least in certain situations. However, what if you wanted a
closure to access a nonlocal variable that changes - acting differently
depending on what has since happened in the parent function.
That may not be as common, but it is a perfectly plausible situation,
and the hack required to support that behaviour in a Python that acts as
you had expected it to, a surrogate namespace using a class, list, or
dict, is infinitely more cryptic and ugly than the default-parametre hack.
More information about the Python-list