Closures

A function defined inside another function can use the outer function's variables. When the outer function returns the inner one, those variables stay alive, attached to it. That inner function is a closure.

In the program, make_multiplier(3) returns a function that remembers factor = 3. Each call to make_multiplier makes a new closure with its own factor.

A closure can also keep state between calls. To change a remembered variable, not just read it, use nonlocal, as in make_counter.

Closures are a lightweight alternative to a class with one method. They also power decorators, which are next after an exercise. Interviewers sometimes ask you to "write a counter without a class or a global" to test exactly this.

Run it, then make a second counter and check that the two counters don't share a count.