GNU Info

Info Node: (python2.1-tut.info)Lambda Forms

(python2.1-tut.info)Lambda Forms


Next: Documentation Strings Prev: Arbitrary Argument Lists Up: More on Defining Functions
Enter node , (file) or (file)node

Lambda Forms
------------

By popular demand, a few features commonly found in functional
programming languages and Lisp have been added to Python.  With the
`lambda' keyword, small anonymous functions can be created.  Here's a
function that returns the sum of its two arguments: `lambda a, b: a+b'.
Lambda forms can be used wherever function objects are required.  They
are syntactically restricted to a single expression.  Semantically,
they are just syntactic sugar for a normal function definition.  Like
nested function definitions, lambda forms cannot reference variables
from the containing scope, but this can be overcome through the
judicious use of default argument values, e.g.

     >>> def make_incrementor(n):
     ...     return lambda x, incr=n: x+incr
     ...
     >>> f = make_incrementor(42)
     >>> f(0)
     42
     >>> f(1)
     43
     >>>


automatically generated by info2www version 1.2.2.9