O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The lambda Operator

To create an anonymous function in the form of an expression, use the lambda statement:

lambda args : expression

args 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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required