python - How the return result like this , What is the lambda function do in this code? -


this question has answer here:

flist = []  in range(3):      flist.append(lambda: i)  f in flist:     print f() 

i don't know why returning 2,2,2

in last iteration through first loop, i value 2 , hence i values in each element of list 2. because have created live reference i variable.

here simplified demonstration:

a = 5 c = lambda: a += 5 >>> c() 10 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -