all Python 3astterm
ast.Lambda
functions and classes
The node for a lambda expression: args is an arguments node, body a single expression (not a list of statements); the node has no name.
Syntax
ast.Lambda(args, body)
The body is exactly one expression, so a Lambda has no return: the result is body itself. 'f = lambda x: x' is an Assign with a Lambda in value, and the name f is not attached to the node: tools listing functions via FunctionDef skip such ones. Parameters work as in def: the same arguments node with defaults.
Examples
import ast
a = ast.parse('f = lambda x, y=2: x * y').body[0]
lam = a.value
print(type(a).__name__, type(lam).__name__) # → Assign Lambda
print([p.arg for p in lam.args.args], type(lam.body).__name__) # → ['x', 'y'] BinOp
See also
Open in the full glossarydocs.python.orgReport an inaccuracy