Consider the following code:
from threading import Thread
def foo(s):
print(s)
d = {}
for s in ["a", "b"]:
fun = lambda: foo(s)
d[s] = Thread(target=fun)
[worker.start() for worker in d.values()]
I would expect that the lambda expression can not be changed after it got constructed, hence one of the threads needs to print an a
, but that is not happening. Both threads are printing b
. What IS happening here? What needs to be changed to have workload a
included?