February 2006
Intermediate to advanced
648 pages
14h 53m
English
To create an anonymous function in the form of an expression, use the lambda statement:
lambda args : expressionargs is a comma-separated list of arguments, and expression is an expression involving those arguments. For example:
a = lambda x,y : x+y print a(2,3) # produces 5
The code defined with lambda must be a valid expression. Multiple statements and other non-expression statements, such as print, for, and while, cannot appear in a lambda statement. lambda expressions follow the same scoping rules as functions.