Lambdas and variables

Terry Reedy tjreedy at udel.edu
Thu Jul 29 21:50:34 EDT 2004


"John Fouhy" <jfouhy at paradise.net.nz> wrote in message
news:c0f3aa87.0407281914.50f5d62d at posting.google.com...
> [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?

To the best of my knowledge,

func = lambda <params>: <expression>

is almost exactly equivalent to and an abbreviation for

def func(<params>): return <expression>

(The main difference that I know of is in func.__name__ and the consequent
difference in repr(func).  If someone knows of something more of
significance, please tell me.)  So, if you understand simple functions, you
can understand lambdas -- without asking ;-).

Terry J. Reedy






More information about the Python-list mailing list