Python Mastery & Linear Algebra for AIClosures, Decorator Factories & functools.wraps
Video Lesson26 minutes

Closures, Decorator Factories & functools.wraps

Lesson Summary: Cell objects, free variables, creating parameterized decorators, and preserving function introspection metadata.

A closure is a function object that remembers values in enclosing lexical scopes even if they are no longer in memory.

from functools import wraps
import time

def timing_decorator(threshold_ms: float = 100.0):
    def decorator(fn):
        @wraps(fn)
        def wrapper(*args, **kwargs):
            start = time.perf_counter()
            result = fn(*args, **kwargs)
            elapsed = (time.perf_counter() - start) * 1000
            if elapsed > threshold_ms:
                print(f"[WARN] {fn.__name__} took {elapsed:.2f}ms (threshold: {threshold_ms}ms)")
            return result
        return wrapper
    return decorator

@timing_decorator(threshold_ms=50.0)
def compute_data():
    time.sleep(0.06)
    return "Complete"

compute_data()