A closure can retain access to a name from an enclosing scope after that scope returns.
Background
Python programs operate on objects through names. These examples refresh practical reasoning about values, state, interfaces, and scientific calculations. Run them in a disposable local environment and inspect the result.
A closure can retain access to a name from an enclosing scope after that scope returns.
Work through an example
A function that constructs a multiplier can return an inner function using the enclosing factor. Calling the returned function later still uses that binding. In a loop, several returned functions can instead refer to the same changing loop variable.
Check the reasoning
Capturing a binding and storing a separate value are different operations. A default parameter evaluated at function definition can preserve a loop value for that function, but mutable default objects create another sharing problem.
Practice independently
Create three functions inside a loop, call them after the loop, and predict their results. Compare ordinary capture with a default-argument version.
Write down your prediction before consulting the result or revisiting the passage. Explain which observation supports your answer and which part depends on an assumption. Revise the explanation if the evidence does not fit. A useful study note records both the conclusion and the steps another learner would need to check it.
Reference
For the language or library contract, consult the Python language tutorial.
Continue with the course
Explore the curriculum, preparation, and study format for Python Refresher — Course Articles.
Companion article for Python Refresher — Course Articles. Original commentary and examples © 2026 Choral Software. Source literary works retain their own attribution.
