An inner function can use names from its enclosing function. That retained environment is a closure. It is useful when you want a small function configured with a multiplier or a counter that remembers its previous result. Python looks up a captured loop variable when a closure runs, which can make several callbacks use the final loop value. Bind the value in a factory or default argument when each callback needs a distinct setting. To reassign a name in an enclosing function, declare it nonlocal. You'll build callbacks and counters, then use them on new inputs. The tasks call the returned helpers immediately inside the entry function so every result remains JSON-compatible.