I just ran across Eric Lippert’s Closing over the loop variable considered harmful via SO, and, after experimenting, realized that the same problem exists (and is even harder to get around) in Python.
>>> l = []
>>> for r in range(10):
... def foo():
... return r
... l.append(foo)
...
>>> for f in l:
... f()
...
9
9
9
# etc
and, the standard C# workaround doesn’t work (I assume because of the nature of references in Python)
>>> l = []
>>> for r in range(10):
... r2 = r
... def foo():
... return r2
... l.append(foo)
...
>>> for f in l:
... f()
...
9
9
9
# etc
I recognize that this isn’t much of a problem in Python with its general emphasis on non-closure object structures, but I’m curious if there is an obvious Pythonic way to handle this, or do we have to go the JS route of nested function calls to create actually new vars?
>>> l = []
>>> for r in range(10):
... l.append((lambda x: lambda: x)(r))
...
>>> for f in l:
... f()
...
0
1
2
# etc
One way is to use a parameter with default value:
yields
This works because it defines an
rinfoo‘s local scope, and binds the default value to it at the timefoois defined.Another way is to use a function factory:
This works because it defines
rinmake_foo‘s local scope, and binds a value to it whenmake_foo(r)is called. Later, whenf()is called,ris looked-up using the LEGB rule. Althoughris not found in the local scope offoo, it is found in the enclosing scope ofmake_foo.