Re: Lambda evaluation

2005-10-07 Thread Eric Nieuwland
Joshua Ginsberg wrote: > Try this one: > d = {} for x in [1,2,3]: > ... d[x] = lambda *args: args[0]*x > ... d[1](3) try it with: d[x] = (lambda x=x: (lambda *args: args[0]*x))() the outer lambda fixes the value of x and produces the inner lambda with the fixed x value

Re: Lambda evaluation

2005-10-06 Thread Joshua Ginsberg
That's a damned elegant solution -- thank you... However in trying to simplify my problem for presentation here, I think I oversimplified a bit too much. Try this one: >>> d = {} >>> for x in [1,2,3]: ... d[x] = lambda *args: args[0]*x ... >>> d[1](3) 9 The lambda is going to have to take ar

Re: Lambda evaluation

2005-10-06 Thread Duncan Booth
Jp Calderone wrote: > On Thu, 06 Oct 2005 16:18:15 -0400, Joshua Ginsberg > <[EMAIL PROTECTED]> wrote: >>So this part makes total sense to me: >> > d = {} > for x in [1,2,3]: >>... d[x] = lambda y: y*x >>... > d[1](3) >>9 >> >>Because x in the lambda definition isn't evaluated unt

Re: Lambda evaluation

2005-10-06 Thread Steve M
Here's another one: >>> d = {} >>> for x in [1,2,3]: ... d[x] = (lambda z: lambda y: y * z) (x) ... >>> d[1](3) 3 >>> d[2](3) 6 -- http://mail.python.org/mailman/listinfo/python-list

Re: Lambda evaluation

2005-10-06 Thread Fredrik Lundh
Joshua Ginsberg wrote: > So this part makes total sense to me: > > >>> d = {} > >>> for x in [1,2,3]: > ... d[x] = lambda y: y*x > ... > >>> d[1](3) > 9 > > Because x in the lambda definition isn't evaluated until the lambda is > executed, at which point x is 3. > > Is there a way to specifica

Re: Lambda evaluation

2005-10-06 Thread Jp Calderone
On Thu, 06 Oct 2005 16:18:15 -0400, Joshua Ginsberg <[EMAIL PROTECTED]> wrote: >So this part makes total sense to me: > d = {} for x in [1,2,3]: >... d[x] = lambda y: y*x >... d[1](3) >9 > >Because x in the lambda definition isn't evaluated until the lambda is >executed, at which

Lambda evaluation

2005-10-06 Thread Joshua Ginsberg
So this part makes total sense to me: >>> d = {} >>> for x in [1,2,3]: ... d[x] = lambda y: y*x ... >>> d[1](3) 9 Because x in the lambda definition isn't evaluated until the lambda is executed, at which point x is 3. Is there a way to specifically hard code into that lambda definition the c