list of lambda

Paul Rubin http
Fri Nov 11 21:28:22 EST 2005


jena <jena at vlakosim.com> 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']]



More information about the Python-list mailing list