Meaning
Creates an anonymous function restricted to a single expression, which is evaluated and returned when the function is called. Addresses the boilerplate of defining a named def block for trivial one-line logic. Reached for when a short, inline callable is needed as an argument to a higher-order function such as map, filter, or sorted.
Primary Function
Anonymous function creation
Communicative Purpose
Provides a concise way to define a small, throwaway function without needing a full def statement.
Pattern
lambda parameters: expression
Core Structure
lambda ...: ...
Função primária
Anonymous function creation
Propósito comunicativo
Provides a concise way to define a small, throwaway function without needing a full def statement.
Situações de gatilho
Data processing: passing a simple transformation to map() or filter(); Sorting: providing a key extraction function to sorted() or list.sort(); Callbacks: supplying a short predicate or transform to GUI event handlers or API wrappers
Contextos
Python scripts, data processing pipelines, functional-style code, testing mocks.
Padrão
lambda parameters: expression
Estrutura central
lambda ...: ...
Slots de substituição
parameters: comma-separated parameter names (e.g., x or x, y); expression: single Python expression that is evaluated and returned
Colocados típicos
- Used with map
- filter
- sorted key
- functools.reduce
- lambda in list comprehensions.
Substituições comuns
- def function_name(param): return expression
- using operator.mul or functools.partial.
Erros comuns
Omitting the colon between parameters and body (syntax error → SyntaxError at parse time); Writing multiple statements in a lambda body (misconception that lambda is a shorthand def → SyntaxError); Capturing a loop variable by reference in a closure (misconception about late binding → all closures see the final loop value); Using assignment or statement syntax inside lambda (misconception about expression vs statement → SyntaxError)
Similar / contraste
def statement for named functions; operator.itemgetter/attrgetter for simple attribute access; functools.partial for fixing arguments.
Interferências
Coming from JavaScript: arrow function uses => syntax → Python lambda uses a colon (lambda x: expr); Coming from Java: anonymous inner classes allow multiple statements → Python lambda is restricted to a single expression with no statements or assignments
Família do chunk
- lambda
- anonymous functions
- higher-order functions
- callback patterns
Nuance
Lambda can contain only a single expression; late-binding closure issues if referencing loop variables; not suitable for complex logic.
Efeito pragmático
Enables concise inline functions, reducing boilerplate for simple callbacks.
Dica de memória
A lambda is a vending-machine function—insert parameters, get one expression back, no kitchen (statements) allowed inside.
Nota
Lambda expressions are limited to a single expression and cannot contain statements.
Upgrade path
Named def function for multi-statement logic or improved readability; functools.partial for pre-binding arguments; operator module for common operations like itemgetter or attrgetter
Log in to save chunks.