Let's say I want a family of functions f[0], f[1], ... f[9]
such that f[y](x) = x + 2 * y
.
To achieve this, I try the following list comprehension:
fs = [(lambda x : x + 2 * y) for y in range(10)]
However, this does not work at all. For example, fs[0](2)
evaluates to 20
, instead of the desired 4
.
What is going on here? Is this some strange scoping issue?
I have also tried things like [(lambda x: x + 2 * y) for y in the list(range(10))]
but none of this helps.