Am 31.05.2011 12:08 schrieb Jussi Piitulainen:

The same sharing-an-i thing happens here:

fs = []
for i in range(4):
...    fs.append(lambda n : i + n)
...
fs[0](0)
3

And the different private-j thing happens here:

gs = []
for i in range(4):
...    gs.append((lambda j : lambda n : j + n)(i))
...
gs[0](0)
0

There is a simpler way: with

>>>> fs = []
>>>> for i in range(4):
> ...    fs.append(lambda n, i=i: i + n)
> ...

you give each lambda a different default argument.

>>>> fs[0](0)
> 0


Thomas
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to