It's being done to bind the value of worker
inside the lambda. Here's a simplified example of this technique:
>>> thunks = [lambda: i for i in range(5)]
>>> [thunk() for thunk in thunks]
[4, 4, 4, 4, 4]
>>>
>>> thunks = [lambda i=i: i for i in range(5)]
>>> [thunk() for thunk in thunks]
[0, 1, 2, 3, 4]
With the expression lambda: i
, i
is evaluated at the time the lambda is called, not when it is defined. Hence in the first example, the results from all of the thunks are 4
because that's the value that i
has at the end of the range(5)
loop.
With the expression lambda i=i: i
, now the value of i
is being evaluated immediately within the loop in order to provide the default value of the i
parameter. This allows each thunk to capture a unique value of i
.
The concept might be more clear if the parameter is given a different name instead of shadowing i
:
>>> thunks = [lambda n=i: n for i in range(5)]
>>> [thunk() for thunk in thunks]
[0, 1, 2, 3, 4]
In your example, the lambda could be written as:
worker_fn = lambda w=worker: w.run(sess, coord, FLAGS.t_max)
This behaves the same as the worker=worker: worker.run...
expression in your code, but might make it a little more clear that the purpose of the expression is to take the current value of worker
in the loop and pass it into the body of the lambda as a parameter.