Under The Hood
backend python CPython 3.12+

Decorators: Closure Binding, Parameterized Wrappers, and functools.wraps

Last updated
Prerequisites:
Python First-Class Functions and Closures
Scope resolution (`*args`, `**kwargs`)
  • python
  • decorators
  • functools
  • wraps
  • closures
  • metaprogramming

Read at your depth

The practical view

A decorator is a function that takes another function as an argument and returns an augmented function: `@my_decorator def hello(): ...` is syntactic sugar for `hello = my_decorator(hello)`. Parameterized decorators require an extra outer function layer: `@repeat(num=3)`. Always use `@functools.wraps(fn)` on your wrapper function to preserve the original function's name (`__name__`), docstring (`__doc__`), and type annotations.

The same idea in other frameworks

python equivalent
`@decorator` syntax with `functools.wraps(fn)`.

Legacy vs modern

Naive Decorator (Lost Metadata) vs functools.wraps Decorator

Naive decorators obliterate `__name__` and `__doc__`; `@functools.wraps` preserves metadata and the `__wrapped__` pointer.

before → after
Naive Decorator (Broken Metadata)
def wrapper(*args, **kwargs): ... return wrapper // ❌ Overwrites __name__ with 'wrapper'
Production Decorator with @functools.wraps
@functools.wraps(fn)
def wrapper(*args, **kwargs): ... // ✅ NEW: Preserves name, docstring & signature

Interview gotchas

Context

Interviewers test your knowledge of `inspect` and un-wrapping hooks.

The mechanical answer

Access the **`__wrapped__`** attribute: `original_fn = decorated_fn.__wrapped__`. `@functools.wraps` automatically attaches a reference to the original function on `__wrapped__`, allowing unit tests and dependency injectors to bypass middleware or timing wrappers during testing.

Trap

If a function is decorated with multiple chained decorators, `__wrapped__` points to the immediately preceding wrapper; use `inspect.unwrap(fn)` to unwrap all chained layers completely.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.