Consider the following Python 3 instructions
res = []
for a in range(3) :
res.append(lambda n : n + a)
with the aim of building a list res
of three functions res[0]
, res[1]
, and res[2]
such that res[i](n)
returns n + i
, for all i
in [0, 1, 2]
.
Nevertheless, one obtains
>>> res[0](0)
2
instead of
>>> res[0](0)
0
One also have
>>> res[1](2)
4
The explanation of this behavior is that in the expression n + a
in the body of any dynamically generated anonymous function of the example, the symbol a
is not evaluated at the creation of the function. The evaluation is performed at the exit of the for statement, explaining why all functions res[0]
, res[1]
, and res[2]
return the value of their argument plus 2è (because
abrowses
range(3)and
2` is its last value).
Notice that the problem does not lie in the fact that we use anonymous functions. Indeed, the instructions
res = []
for a in range(3) :
def f(n) :
return n + a
res.append(f)
lead to the same behavior.
Notice also that we can meet the objective set out above by using the function eval
of Python:
res = []
for a in range(3) :
s = "lambda n : n + %s" %(a)
res.append(eval(s))
The trick lies on the fact that the dynamic value of a
is considered for the creation of each function of res
.
Now my questions are
- is this a bug or a feature?
- is there an other way not passing by
eval
to obtain the expected behavior?