Lambdas and variables

John Fouhy jfouhy at paradise.net.nz
Wed Jul 28 23:14:08 EDT 2004


[original post: http://groups.google.co.nz/groups?hl=en&lr=&ie=UTF-8&selm=c0f3aa87.0407271535.40217366%40posting.google.com
]

Derek Thomson <derek.thomson at gmail.com> wrote in message news:<mailman.861.1090973013.5135.python-list at python.org>...
> It's to do with the environment to which a function, or a lambda, is
> bound. "x" in the lambda will always refer to the x defined in the
> enclosing scope, and you last left that set to "zof".

Ok, so when I write 'lambda e: printSomething(x)', that creates a
function which, when called, will look up the value of the variable
'x', and then call printSomething on that value.  And that value will
be whatever x was last set to.  Is that correct?

What if I do 'lambda e: printSomething(copy.deepcopy(x))'?  That
creates a function which, when called, will look up the value of 'x',
make a deep copy, and then call printSomething on it?

('cos that doesn't work either)

If what I wrote is correct, then I think I understand now.  And I will
use the change James Henderson suggested.  But it definitely feels
like a gotcha if I can't replace a function that just does 'return X'
with 'X' itself.

(thanks for your help, guys)

-- 
John.



More information about the Python-list mailing list