lambda without vars?

François Pinard pinard at iro.umontreal.ca
Wed Mar 15 10:26:33 EST 2000


moonseeker at my-deja.com writes:

> Can't I use variables in a lambda definition?

> xList = [5, 10, 20, 35, 55]
> y = 50
> a = filter (lambda x: x<y, xList)

Yes, using this trick:

   a = filter(lambda x, y=y: x<y, xList)

However, you cannot have the function to modify such variables (that is,
the value they have outside lambda).

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard






More information about the Python-list mailing list