Featurecore
Closures and nonlocal
Also called closure, closure variable, free variable, nested function, inner function, function factory, __closure__, late binding closure, counter with nonlocal.
A nested function can use the variables of the function that contains it, and it keeps them after the outer call returns. To assign to such a variable, the nested function declares it nonlocal.
- Language
- Python
Synopsis
def make_counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
c = make_counter()
print(c(), c(), c())
d = make_counter()
print(d())
1 2 3
1
increment keeps count after make_counter returns. Each call of make_counter makes a new count.
Semantics
r1. "If a variable is used in a code block but not defined there, it is a free variable."2
r2. A closure variable is "A free variable referenced from a nested scope that is defined in an outer scope rather than being resolved at runtime from the globals or builtin namespaces. May be explicitly defined with the nonlocal keyword to allow write access, or implicitly defined if the variable is only being read."1 Example: read-only.
r3. "If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name."2
r4. "If a name is bound in a block, it is a local variable of that block, unless declared as nonlocal or global."2 Example: no-nonlocal.
r5. "The nonlocal statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope. SyntaxError is raised at compile time if the given name does not exist in any enclosing function scope."2 Example: no-enclosing.
r6. A function has the attribute __closure__: "None or a tuple of cells that contain bindings for the names specified in the co_freevars attribute of the function’s code object."3 Example: cells.
Examples
def make_multiplier(factor):
def multiply(x):
return x * factor
return multiply
double = make_multiplier(2)
triple = make_multiplier(3)
print(double(5), triple(5))
10 15
multiply only reads factor, so it needs no nonlocal (rule r2).
def outer():
x = 0
def inner():
nonlocal x
x += 1
return x
return inner
f = outer()
f()
f()
print(f.__code__.co_freevars, f.__closure__[0].cell_contents)
('x',) 2
This is the example of the glossary. The cell holds the current value of x (rule r6).
Pitfalls
An assignment in the nested function makes a new local name, so += on the outer name fails:
This code raises UnboundLocalError, on purpose.
def make_counter():
count = 0
def increment():
count += 1
return count
return increment
make_counter()()
count += 1 binds count, so count is local to increment (rule r4). Add nonlocal count.
nonlocal needs a variable in an enclosing function, not a global:
This code raises SyntaxError, on purpose.
total = 0
def add(n):
nonlocal total
total += n
A closure reads the variable, not its value at the time when the closure was made:
This code runs but gives a wrong answer on purpose.
functions = []
for i in range(3):
def show():
return i
functions.append(show)
print([f() for f in functions])
[2, 2, 2]
The FAQ explains this for "lambdas (or even plain functions)": the variable "is accessed when the lambda is called".4 Pass the value as a default, def show(i=i), to keep the current value.
Comparison
Go function literals are also closures: "Function literals are closures: they may refer to variables declared in a surrounding function. Those variables are then shared between the surrounding function and the function literal, and they survive as long as they are accessible."5 A Go closure can assign to such a variable with no extra keyword. A Python nested function needs nonlocal to assign to it (rule r4).
References
- Glossary, Python 3.15, closure variable; free variable
- The Python Language Reference, Python 3.15, 4.2.1. Binding of names; 4.2.2. Resolution of names
- The Python Language Reference, Python 3.15, 3.2.8.1.1. Special read-only attributes
- Programming FAQ, Python 3.15, Why do lambdas defined in a loop with different values all return the same result?
- The Go Programming Language Specification, Function literals
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag THZ00V Edit this page History Report an error Markdown