Linux/Unix > Python Essential Reference, Second Edition > 6. Functions and Functional Programming > The lambda Operator | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137104017178094161143100029

### 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 nonexpression statements such as `print`, `for`, and `while` cannot appear in a `lambda` statement. `lambda` expressions follow the same scoping rules as functions.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.