Skip to Content
Python: Essential Reference, Third Edition
book

Python: Essential Reference, Third Edition

by David Beazley
February 2006
Intermediate to advanced content levelIntermediate to advanced
648 pages
14h 53m
English
Sams
Content preview from Python: Essential Reference, Third Edition

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python: Essential Reference

Python: Essential Reference

David M. Beazley

Publisher Resources

ISBN: 0672328623Purchase book