Re: list of lambda

2005-11-11 Thread Bengt Richter
On Sat, 12 Nov 2005 14:55:31 +1100, Steven D'Aprano <[EMAIL PROTECTED]> wrote: >On Sat, 12 Nov 2005 00:17:59 +0100, jena wrote: > >> hello, >> when i create list of lambdas: >> l=[lambda:x.upper() for x in ['a','b','c']] >> then l[0]() returns 'C', i think, it should be 'A' > >What is wrong with j

Re: list of lambda

2005-11-11 Thread Bengt Richter
On 11 Nov 2005 18:28:22 -0800, Paul Rubin 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

Re: list of lambda

2005-11-11 Thread Steven D'Aprano
On Sat, 12 Nov 2005 00:17:59 +0100, jena wrote: > hello, > when i create list of lambdas: > l=[lambda:x.upper() for x in ['a','b','c']] > then l[0]() returns 'C', i think, it should be 'A' What is wrong with just doing this? L = [x.upper() for x in ['a', 'b', 'c']] py> L = [lambda: x.upper()

Re: list of lambda

2005-11-11 Thread [EMAIL PROTECTED]
Leif K-Brooks wrote: > jena wrote: > > hello, > > when i create list of lambdas: > > l=[lambda:x.upper() for x in ['a','b','c']] > > then l[0]() returns 'C', i think, it should be 'A' > > Fredrik Lundh provided the general solution, but in this specific case, > the simplest solution is: > > l = [x

Re: list of lambda

2005-11-11 Thread Paul Rubin
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']] -- http://mail.python.or

Re: list of lambda

2005-11-11 Thread Leif K-Brooks
jena wrote: > hello, > when i create list of lambdas: > l=[lambda:x.upper() for x in ['a','b','c']] > then l[0]() returns 'C', i think, it should be 'A' Fredrik Lundh provided the general solution, but in this specific case, the simplest solution is: l = [x.upper for x in ['a', 'b', 'c']] -- htt

Re: list of lambda

2005-11-11 Thread Fredrik Lundh
"jena" <[EMAIL PROTECTED]> wrote: > when i create list of lambdas: > l=[lambda:x.upper() for x in ['a','b','c']] > then l[0]() returns 'C', i think, it should be 'A' the "x" variable contains "c" when you leave the loop: >>> l=[lambda:x.upper() for x in ['a','b','c']] >>> x 'c' so x.upper() wil