warming up your workspace

Closure

Applies to: general, python

A closure is a function that remembers variables from the scope where it was defined, even after that scope has returned. It is how you build configurable functions and is the basis of decorators and callbacks.

def multiplier(n):
    def times(x):
        return x * n   # remembers n
    return times
double = multiplier(2)

See also: function, scope