W
wyleu
I'm trying to supply parameters to a function that is called at a
later time as in the code below:
llist = []
for item in range(5):
llist.append(lambda: func(item))
def func(item):
print item
for thing in llist:
thing()
which produces the result
IDLE 1.2.1<function <lambda> at 0xb716356c>
<function <lambda> at 0xb71635a4>
<function <lambda> at 0xb71635dc>
How can one allocate a different parameter to each instance of the
function rather than all of them getting the final value of the loop?
later time as in the code below:
llist = []
for item in range(5):
llist.append(lambda: func(item))
def func(item):
print item
for thing in llist:
thing()
which produces the result
IDLE 1.2.1<function <lambda> at 0xb716356c>
<function <lambda> at 0xb71635a4>
<function <lambda> at 0xb71635dc>
4
4
4
4
4
How can one allocate a different parameter to each instance of the
function rather than all of them getting the final value of the loop?