PyCodeItPython trace & execution models
Python Mastery Hub/lambda expressions
Python 3.12+ CPython TrackPyodide WASM Sandbox

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.

Anonymous Function Compilation & Closure Late-Binding

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.

Late Binding in Loops and Default Argument Snapshots

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

Problem 1 / 61+50 XP

The Late Binding Closure Trap

hard
Task: Output Execution Prediction

Trace CPython execution step-by-step and deduce the exact stdout string emitted by this snippet.

Socratic Guidance-5 XP penalty per revealed hint
Topic Problems0 / 61 Solved
main.pyCPython 3.12 (WASM)
1
2
funcs = [lambda: i for i in range(3)]
print([f() for f in funcs])
Press Enter to submit
Match exact whitespace and capitalization

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.