Lambda Expressions & Functional Closures
Master anonymous functions, late binding traps, scope resolution, recursive lambdas, and functional pipeline mechanics.
How Lambda Expressions & Functional Closures behaves during execution
Lambda expressions create small, anonymous, single-expression function objects at runtime. In CPython, a lambda expression produces a standard function object (types.FunctionType) that returns the evaluated result of its single expression without a return keyword. While useful for short inline callbacks (such as sort key functions), lambdas cannot contain statements, assignments, or annotations.
A lambda expression is compiled into a code object just like a standard def function. When defined inside a loop or enclosing scope, the lambda creates a closure referencing the variable names in the parent frame, rather than copying their current values. Evaluating the lambda at a later time reads the final value of the referenced variable from the enclosing scope.
Creating a list of lambdas in a loop (e.g. [lambda: i for i in range(3)]) causes every lambda to share a reference to the same variable i. When executed after the loop, all lambdas return the loop's final value (2). To snapshot the value at definition time, bind it as a default parameter: lambda i=i: i.
How to use this tool
Read the server-rendered examples above first, write down each variable change in a trace table, then use the interactive workspace below to step through execution. Tracing builds clear mental models for technical interviews.
Worked example
# Trace the execution line by line x = 10 y = x * 2 print(x + y)
Static result: 30
Controls
The code panel shows the snippet; Run executes it in the browser Pyodide sandbox; Next and Previous step through line states; the result panel displays output and state changes.
Honest limits
Python code executes locally inside your browser WebAssembly (Pyodide) sandbox. Execution is client-side, rate-free, and isolated from our servers. Do not enter credentials or private API keys.
Continue with the Python Mastery Hub.
Interactive Dry-Run Execution Workspace
The Late Binding Closure Trap
Trace CPython execution step-by-step and deduce the exact stdout string emitted by this snippet.
funcs = [lambda: i for i in range(3)]
print([f() for f in funcs])Frequently Asked Questions on Lambda Expressions & Functional Closures
Why do lambdas created in a loop all return the same final value?
Python closures bind to variable names, not values. By the time the lambdas are executed, the loop has completed and the shared variable retains its final iteration value. Pass the variable as a default argument (lambda i=i: i) to snapshot it.
What are the structural limitations of Python lambda functions compared to def?
Lambdas are restricted to a single syntactic expression whose value is returned automatically. They cannot contain statements (such as while, for, try/except), assignments, type annotations, or docstrings.
How does lambda performance compare to a named function defined with def?
Bytecode and runtime performance are virtually identical. Both create a FunctionType object with an underlying code object. The primary distinction is that named functions provide clearer traceback names and documentation.