Closures and Scopes

May 30, 2026 | 5 min read

Closure Basics

def make_multiplier(factor: float):
    def multiply(x: float) -> float:
        return x * factor
    return multiply
double = make_multiplier(2.0)

Partial Application

from functools import partial

def power(base: float, exp: float) -> float:
    return base ** exp

square = partial(power, exp=2)
cube = partial(power, exp=3)

Factory Functions

def make_validator(min_val: int, max_val: int):
    def validate(x: int) -> bool:
        return min_val <= x <= max_val
    return validate