Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Lambda evaluation

Reply
Thread Tools

Re: Lambda evaluation

 
 
Jp Calderone
Guest
Posts: n/a
 
      10-06-2005
On Thu, 06 Oct 2005 16:18:15 -0400, 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 specifically hard code into that lambda definition the
>contemporary value of an external variable? In other words, is there a
>way to rewrite the line "d[x] = lambda y: y*x" so that it is always the
>case that d[1](3) = 3?


There are several ways, but this one involves the least additional typing:

>>> d = {}
>>> for x in 1, 2, 3:

... d[x] = lambda y, x=x: y * x
...
>>> d[1](3)

3

Who needs closures, anyway?

Jp
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      10-06-2005
Jp Calderone wrote:

> On Thu, 06 Oct 2005 16:18:15 -0400, 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 specifically hard code into that lambda definition
>>the contemporary value of an external variable? In other words, is
>>there a way to rewrite the line "d[x] = lambda y: y*x" so that it is
>>always the case that d[1](3) = 3?

>
> There are several ways, but this one involves the least additional
> typing:
>
> >>> d = {}
> >>> for x in 1, 2, 3:

> ... d[x] = lambda y, x=x: y * x
> ...
> >>> d[1](3)

> 3
>
> Who needs closures, anyway?
>


Just for completeness, here's the lambda free closure version:

>>> def timesx_factory(x):

def timesx(y):
return y * x
return timesx

>>> d = dict((x, timesx_factory(x)) for x in range(1,4))
>>> d[1](3)

3
>>> d[2](3)

6
>>>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type of lambda function returning a lambda function... Haochen Xie C++ 4 03-17-2013 11:23 PM
lambda vs non-lambda proc Steve Dogers Ruby 1 03-30-2009 10:11 PM
Lambda evaluation Joshua Ginsberg Python 1 10-06-2005 08:52 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
Re: Lambda as declarative idiom (was RE: what is lambda used for inreal code?) Roman Suzi Python 13 01-07-2005 09:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57