On 11 Nov 2005 18:28:22 -0800, Paul Rubin <http://[EMAIL PROTECTED]> wrote:
>jena <[EMAIL PROTECTED]> writes: >> l=[lambda:x.upper() for x in ['a','b','c']] >> then l[0]() returns 'C', i think, it should be 'A' > >Yeah, this is Python late binding, a standard thing to get confused >over. You want: > > l = [lambda x=x: x.upper() for x in ['a', 'b', 'c']] or if you want the upper() eagerly (and do it only once each, in case of multiple lambda calls) l = [lambda x=x.upper():x for x in ['a', 'b', 'c']] >>> l = [lambda x=x.upper():x for x in ['a', 'b', 'c']] >>> for lamb in l: print lamb.func_defaults[0],'=?=',lamb() ... A =?= A B =?= B C =?= C Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list