Trouble using lambda to pass args to event function

Nitro wrote:

I've run into this already, too :slight_smile: It's actually a python issue. "contact" always refers to the "contact" of the last loop iteration.

Thanks for the quick reply, but I don't think that's the problem - I've just changed 'contact' to 'person', and the issue prevails!

Take a look at this sample program:

a = [1,2,3]
fns = [lambda x: item for item in a]
fns[0](1)

3

You would expect it to return 1, but it returns 3. That's because item is a reference. So your program really looks like

fns[i]: when I am called evaluate and return item

Now, when you call fns[0], then item will be "evaluated" to 3, because that's its value after the list comprehension. The same happens with fns[1] and fns[2].
I agree this is absolutely unintuive, but this is the way references work. I am not sure about the best workaround, so it would be nice if others post some suggestions.

-Matthias

···

Am 29.11.2006, 00:19 Uhr, schrieb Tom Taylor <tom@tomtaylor.co.uk>: