The problem is that you are only ever evaluating ev at the time you call the function. Thus, it's using whatever value ev
has only when you start the print statement. Of course, by that time ev
has the value of the last function in the list.
This is no different than if you had done this:
funcs = [lambda x: ev.eval(x+0.1),
lambda x: ev.eval(x+0.1),
lambda x: ev.eval(x+0.1)]
Notice how they all use ev
, and they will all use the same ev
at the time you run the functions.
To do what you want, you need to bind ev
to its current value in the list comprehension at the time you define the comprehension, which you can do by passing in the value through the lambda arguments:
funcs = [lambda x, ev=ev: ev.eval(x+0.1) for ev in (ev1, ev2, ev3)]
However, I strongly suggest you do not do this. As you have just experienced, such code is very hard to understand and debug. You win no points for cramming as much functionality into a single line as possible.
The technical term for this is a closure. For more information, check out this question on stackoverflow: Why aren't python nested functions called closures?