On Tue, May 31, 2011 at 12:48 AM, harrismh777 <harrismh...@charter.net> wrote: > What is going on with the binding in the first construct... this seems > to reduce the usefulness of lambda to a considerable extent?
I don't see why; as you've shown there are a couple of simple ways to avoid this problem. The trick is just to recognize that when you have a closure around a variable, and that variable needs to change, but the value in the closure needs to be constant, then what you really need are two separate variables -- the cell variable needs to be promoted to a local. How you accomplish that is not terribly important. One other technique that is sometimes preferable is functools.partial, e.g.: fs = [functools.partial(operator.add, i) for i in range(10)] Tangentially, I'd like to point out that this line: [fs[i](1) for i in range(10)] is more naturally written as: [f(1) for f in fs] -- http://mail.python.org/mailman/listinfo/python-list