lambda semantics in for loop

Henk Punt henk at empanda.net
Sun Jan 5 07:36:39 EST 2003


Hi,

When I have the following bit of python code:

>>> l = []
>>> for i in range(10):
...     l.append(lambda x: x + i)

I would expect:

l[0](0) = 0
l[1](0) = 1

l[0](1) = 1
l[1](1) = 2

etc.

instead

l[0](0) = 9
l[1](0) = 9

l[0](1) = 10
l[1](1) = 10

It seems that the 'i' in the lambda binds to the last value of i in the
for loop.
Is this because 'i' is really a pointer and not the value of 'i' itself?.
Please enlighten me!,

How do I modify the example so that I would get my expected semantics.
Should I copy 'i' to force the creation of a new object?, If so how would
this work in the case where i is a string. I've tried to coerce python
into making a deepcopy of a string so that id(s) != id(copy(s)) but I've
not been able to do that also.

Thanx already,

Henk Punt.




More information about the Python-list mailing list