Lambda evaluation
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Lambda evaluation

From: Joshua Ginsberg <listspam@flowtheory.net>
Date: Thu Oct 06 2005 - 22:18:15 CEST

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?

Thanks!

-jag
Received on Sat Oct 15 04:14:08 2005