Lambda Scoping
Gerson Kurz
gerson.kurz at t-online.de
Mon Apr 29 11:02:42 EDT 2002
The following code
...
funcs = []
for instance in range(10):
funcs.append( lambda: instance )
print map(apply,funcs)
funcs = []
for instance in range(10):
funcs.append( lambda instance=instance: instance )
print map(apply,funcs)
...
prints
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
which I sort-of understand (I figure, in "lambda: instance" instance
is a lazy expression; "lambda instance=instance: instance" the
expression itself - "instance" - is a lazy expression, too, but the
argument list is not.) However, can anybody please explain this more
detailed?
More information about the Python-list
mailing list