Something is rotten in Denmark...
harrismh777
harrismh777 at charter.net
Tue May 31 16:18:37 EDT 2011
Terry Reedy wrote:
> You have been hypnotizeed by lambda. (lambda n: i+n) is a *constant
> expression*, so you get 10 'equal' functions.
'hypnotized' indeed! ... ok, so let me see if I get this... the lambda
defers lookup|bind of its references until such time as the lambda is
'called' and not at the time (as I thought) that the anonymous
function(s) are returned?
If I'm understanding that correctly, then that means lambda is working
as designed, and that there are very subtle nuances to be aware of. In
my little case
(lambda n: i + n)
... if the i goes out of scope before the anonymous function gets
called then we have a problem... or if i as a reference is mutable or
refers to a different object before the anonymous function is called
then we have a problem?
What I am discovering is that 'yes' I can use lambda syntactically
where I might not be able to code a def statement; however, if I do use
it (as in a list comprehension) then I may get unexpected results if any
of my lambda's references go out of scope or are mutable...(?)
Question:
What are the ramifications of making the lookup|binding happen at
the time the anonymous function is 'returned' vs 'called'? Has anyone
suggested this? Is this PEP-able....? Are there side-effects in the
other direction?
PS Thanks Chris, Ian, Jussi, Thomas, Terry... I appreciate your
teaching and patience !
kind regards,
m harris
More information about the Python-list
mailing list