lambda

Mel Wilson mwilson-to at sympatico.ca
Fri Apr 21 15:39:29 EDT 2006


rubbishemail at web.de wrote:
> Hello,
> 
> I need your help understanding lambda (and doing it a better way
> without).
> 
> f  = lambda x : x*x
[ ... ]
> # the idea is now to give the definition of the multiplication of
> functions and integers
> # (f * c)(xx) := f(x)*c
> [lambda xx: f(xx)*y for y in range(1,5)][0](1)
> # returns 4
> # I would expect 1*x*x = 1

I think you have to get the value of y at definition time, 
not at call time:

 >>> z=[lambda xx, yy=y: (f(xx)*yy) for y in xrange(1,5)]
 >>> z[0](1)
1

         Mel.



More information about the Python-list mailing list