6.3.4 Returning Functions & Closures

Unit: Unit 6: Functions • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Create function factories using nested functions and closures
def make_multiplier(factor):
    # Inner function captures and remembers 'factor' from enclosing scope
    def multiplier(number):
        return number * factor
    return multiplier

# Generate specialized multiplier functions
double = make_multiplier(2)
triple = make_multiplier(3)

print("Double 15:", double(15))
print("Triple 15:", triple(15))
print("Double 7:", double(7))
Open in Live Interactive Python Sandbox →