This is due to the point at which d is being bound. The lambda functions all point at the variable d
rather than the current value of it, so when you update d
in the next iteration, this update is seen across all your functions.
For a simpler example:
funcs = []
for x in [1,2,3]:
funcs.append(lambda: x)
for f in funcs:
print f()
# output:
3
3
3
You can get around this by adding an additional function, like so:
def makeFunc(x):
return lambda: x
funcs = []
for x in [1,2,3]:
funcs.append(makeFunc(x))
for f in funcs:
print f()
# output:
1
2
3
You can also fix the scoping inside the lambda expression
lambda bound_x=x: bound_x
However in general this is not good practice as you have changed the signature of your function.