Skip to content

Python Closures, Captured State, and Late Binding

Step 34 of 41

Python Late Binding: Loop Callbacks, Defaults, and partial

Introduction

Python late binding means a function reads a referenced binding when it runs. Callbacks built in a loop can therefore all see the loop’s final value. A parameter default records an object at definition time, while a factory provides a fresh parameter binding for each call.

This lesson combines PCAP-31-03 objectives 5.2 and 5.3 in Comprehensions, Lambdas, Closures, and I/O (22% of the score, 9 of 40 items). You will trace callback timing and choose between a default, a factory, and partial application without confusing capture with copying.

Learning objectives

After this lesson you can:

  1. Explain late binding: a closure reads the captured variable when it is called, not when it is created.
  2. Predict the output of functions or lambdas created in a loop.
  3. Repair the loop with a default argument, a factory function, or functools.partial.
  4. Explain why comprehension variables and loop variables behave differently from factory parameters.
  5. Contrast closures, lambdas with default arguments and ordinary parameters.
  6. Trace repeated calls of closures that share or do not share a variable.
  7. Debug a callback table in which every entry does the same thing.

The problem: all functions return the last value

Consider a program that builds three functions, one for each value of i:

Python
funcs = []
for i in range(3):
    funcs.append(lambda: i)

print([f() for f in funcs])

Output:

Text
[2, 2, 2]

Many learners expect [0, 1, 2]. Let us trace it with the correct model.

  1. The for statement does not create a new scope. There is one variable i in the enclosing scope (here the module) for the whole loop.
  2. Each iteration creates a new lambda. The lambda looks up the module-global i at call time. In this module-level example there is no closure cell for i, although the late-binding result resembles the enclosing-function case below.
  3. When the loop ends, i holds 2, the last value from range(3).
  4. Only then are the functions called. Each one reads i at call time, and finds 2.

The three functions are different objects, but they all look at the same variable. That is why the result is [2, 2, 2].

The same happens with nested def, not only with lambdas:

Python
def late():
    fs = []
    for n in (1, 2, 3):
        def show():
            return n * 10
        fs.append(show)
    return fs

print([f() for f in late()])

Output:

Text
[30, 30, 30]

The loop variable n belongs to late. All three show functions share its cell, and the last assignment was 3.

The same rule with a rebinding after creation

The behaviour is not tied to loops. Any rebinding of the variable before the call is visible:

Python
def outer():
    v = "a"
    def get():
        return v
    v = "b"
    return get

print(outer()())

Output:

Text
b

get was created when v was "a", but v was reassigned to "b" before the function was called. The closure sees the latest value. Likewise, with a global name:

Python
x = 1
def mk():
    return lambda: x

f = mk()
x = 2
print(f())

Output:

Text
2

Three ways to capture a value on purpose

Sometimes you really want each function to remember the value from its iteration. You have to make a separate variable for each function. There are three common techniques.

1. A default argument

Default values are evaluated when the function is defined, that is, at each iteration. The object is saved in the function’s defaults and becomes the parameter value when omitted by a caller. This does not copy the saved object. See shared references and object mutation for how mutation differs from rebinding.

Python
funcs = [lambda i=i: i for i in range(3)]
print([f() for f in funcs])

Output:

Text
[0, 1, 2]

Here i=i means: the parameter named i takes as its default the current value of the outer i. The cost is that the function can now be called with an argument that overrides the default: funcs[0](99) returns 99. That is usually acceptable for a callback but should be documented.

2. A factory function

A factory creates a new scope for every call. The parameter of the factory is a variable that nothing else changes, so each closure has its own.

Python
def make(i):
    return lambda: i

print([make(i)() for i in range(3)])

Output:

Text
[0, 1, 2]

This is the technique from the previous lesson, and it is the cleanest one when the inner function is long. A one-line version uses an immediately called lambda:

Python
adders = [(lambda k: (lambda n: n + k))(k) for k in (1, 10, 100)]
print([a(1) for a in adders])

Output:

Text
[2, 11, 101]

The outer lambda receives the current k as its argument, so it creates a new scope with its own k. It is correct, but harder to read than a named factory.

3. functools.partial

partial builds a new callable with some arguments already fixed. The saved arguments are object references evaluated immediately. Rebinding the caller’s name does not replace the stored reference, but mutating that referenced object can affect a later call.

Python
import functools

fs = [functools.partial(lambda n, i: n + i, i=i) for i in range(3)]
print([f(10) for f in fs])

Output:

Text
[10, 11, 12]

A partial object is not a function with closure cells, but it can bind arguments for the same callback use case. Python 3.14 also adds functools.Placeholder for positional slots to be supplied later; this example uses only the long-established keyword form. The PCAP syllabus focuses on closures and lambdas themselves, so you should be able to read the first two forms quickly and recognise the third.

Technique Where the value is stored Can the caller override it? Typical use
Default argument (lambda i=i: i) Parameter default Yes, by passing an argument Short callbacks
Factory function Cell of the factory call No Longer inner functions
functools.partial Saved argument references Saved keywords can be overridden; positional binding follows the wrapped signature Fixing arguments of an existing function

Comprehensions and loops: what changes?

A for statement at module level or in a function reuses one variable. A list comprehension has its own scope in Python 3, so its loop variable does not leak into the enclosing scope. Both still create one variable for the whole comprehension, though, so lambdas created inside it suffer from late binding as well:

Python
gens = [(lambda: i) for i in range(2)]
i = 99
print([g() for g in gens])

Output:

Text
[1, 1]

Two points. The lambdas refer to the comprehension's variable i, whose last value is 1. The global i = 99 is a different variable, so it has no effect. Many candidates answer [99, 99], because they believe the lambda looks at the global name.

Compare with a function parameter, which is a fresh variable at each call:

Python
def make(i):
    return lambda: i

If this factory is called once for each of three values, those are three separate parameter bindings. That difference between "one variable for the whole loop" and "one variable per call" is the heart of this chapter.

Closures, lambdas and ordinary parameters

It helps to compare the three ways in which a function can obtain the data it uses.

Mechanism When is the value fixed? Can it change later? Example
Ordinary parameter At each call New value for every call def f(x): return x + 1
Default argument Object reference saved at definition time A caller can override the argument, and the saved object can be mutable def f(x, k=2): return x * k
Closure (free variable) Read at call time Yes, if the variable is rebound def mk(k): return lambda x: x * k

A lambda is only a function written as an expression. It can have parameters, defaults and free variables, exactly like a def. Do not think of "lambda closures" as a separate concept: the rules in this lesson apply identically to both.

Reasoning about repeated calls

When closures share a variable through nonlocal, the order of the calls matters. Use a trace table:

Python
def make_counter():
    c = 0
    def inc():
        nonlocal c
        c += 1
        return c
    return inc

c1 = make_counter()
c2 = make_counter()
c1()
c1()
print(c1(), c2())

Output:

Text
3 1

c1 and c2 come from two factory calls: two cells. After three calls of c1, its cell contains 3, while c2's cell still holds 0 and the first call returns 1. Compare with a list that repeats the same closure:

Python
cs = [make_counter()] * 2
cs[0]()
print(cs[1](), cs[0] is cs[1])

Output:

Text
2 True

[x] * 2 repeats the same object twice, so cs[0] and cs[1] are the same closure and share the same counter. Building them separately gives independent counters:

Python
cs = [make_counter() for _ in range(2)]
cs[0]()
print(cs[1]())

Output:

Text
1

Worked example: repairing a callback table

Statement. A small application builds a menu. Each entry must return its own name in upper case. The code is:

Python
buttons = {}
for name in ("save", "open", "quit"):
    buttons[name] = lambda: name.upper()

print({k: v() for k, v in buttons.items()})

Predict the output, explain it, and fix it.

Step 1: find the variable. name is a variable of the module. The lambda reads that module-global name at call time. The name is not stored in a __closure__ cell in this example.

Step 2: find the value at call time. The loop has finished, so name is "quit". The lambdas are called only in the dictionary comprehension, after the loop.

Step 3: predict.

Text
{'save': 'QUIT', 'open': 'QUIT', 'quit': 'QUIT'}

Step 4: repair with a default argument. Change the lambda to lambda name=name: name.upper(). At each iteration, the default is evaluated and stored with the function. The comprehension then produces:

Text
{'save': 'SAVE', 'open': 'OPEN', 'quit': 'QUIT'}

Alternative repair. A factory def make_label(text): return lambda: text.upper() and buttons[name] = make_label(name) has the same effect and avoids the possibility of a caller passing a different argument.

Why other choices are weaker. Calling the lambda inside the loop (lambda: name.upper() followed by ()) would store strings and not functions, so the table would not be callable. Moving the dictionary building into a comprehension does not fix anything: the lambdas would still share one comprehension variable. Renaming the variable has no effect either.

Common pitfalls and exam tips

Pitfall What happens Remedy
Lambdas created in a loop All use the last value Default argument or factory
Expecting a snapshot of the value The value is read at call time Repeat: closures capture variables
[make()] * n to create n closures The same closure n times Use a comprehension to call the factory each time
Believing a global with the same name matters The lambda uses the comprehension variable Find the scope in which the name is bound
Using a default argument with a mutable object The same object is shared by every call Use None and create the object inside
Forgetting that the default argument can be overridden A caller can change the captured value Use a factory when this matters
Calling instead of storing lambda: i is not (lambda: i)() Check parentheses

Exam tip. In a question with a loop that builds functions, first decide when the functions are called. If it is after the loop, and no default argument or factory is used, expect the last value in every function.

Exam tip. A default argument is evaluated once, at definition. So lambda i=i: i inside a loop captures the current value at each iteration.

Exam tip. If the expected answer uses [0, 1, 4] and the code uses def f(i=i), check that the default is really in the signature and not in the body.

Exam tip. A comprehension variable does not leak to the enclosing scope in Python 3, so a later print(i) after [... for i in ...] gives a NameError unless i exists elsewhere.

End-of-lesson case study

Situation. A task scheduler multiplies a value by a delay factor. A developer wrote two versions and wants to know which one is right.

Python
tasks = []
for delay in [1, 2, 3]:
    tasks.append(lambda x: x * delay)
print([t(10) for t in tasks])

tasks = [(lambda x, d=delay: x * d) for delay in [1, 2, 3]]
print([t(10) for t in tasks])

Predict both lines and discuss a safer alternative.

Solution, commented. In the first version each lambda has the free variable delay. After the loop delay is 3, so every task computes 10 * 3: the output is [30, 30, 30]. This is late binding.

In the second version the default d=delay is evaluated while the comprehension runs, so the lambdas hold 1, 2 and 3. The calls compute 10 * 1, 10 * 2 and 10 * 3: [10, 20, 30].

The second version works, but the signature of every task now accepts an optional second argument; a call t(10, 5) would silently ignore the delay chosen when the task was created. A safer approach is a factory:

Python
def make_task(d):
    return lambda x: x * d

tasks = [make_task(d) for d in [1, 2, 3]]

The output is again [10, 20, 30] and the task signature has only the parameter x. Use the factory when the callback contract should expose only x. A partial with a saved keyword can still accept an overriding keyword, so it does not provide the same signature constraint. Compare lambda, map, and filter for the underlying callable rules.

Key terms

  • Late binding: reading a captured variable when the function is called, not when it is created.
  • Default argument capture: using name=name in a signature to store the current value at definition time.
  • Factory function: a function that creates a new scope on each call to produce a configured function.
  • Shared cell: one captured variable referred to by several closures.
  • functools.partial: a callable with some arguments fixed in advance.
  • Comprehension scope: the separate scope of a comprehension, which hides its loop variable from the enclosing code.

Summary checklist

  • I can explain why [lambda: i for i in range(3)] gives three functions that return 2.
  • I know the value is read when the function is called.
  • I can repair late binding with a default argument.
  • I can repair late binding with a factory function.
  • I recognise functools.partial as another way to fix arguments.
  • I know [f] * n repeats one reference, while [f() for _ in range(n)] calls f repeatedly; whether results are distinct depends on f.
  • I can trace several closures with a table, one cell per factory call.
  • I can say when a default argument is a poor choice.

Official sources