This:
l = ['A', 'B', 'C']
l = [lambda: i for i in l]
l = [i() for i in l]
print(l)
results in:
['C', 'C', 'C']
This is clearly due to the binding/scoping rules in Python, but I cannot figure it out. I would be grateful for an explanation?
Furthermore, how do I bind the lambda
to the value of i
in the loop, rather than the variable reference, so that the result is ['A', 'B', 'C']
?