Skip to content
tezvyn:
Menu

Python Decorators: Functions that Wrap Functions

Source: docs.python.orgMediumHow cards are made

Python Decorators: Functions that Wrap Functions

A decorator is a function that wraps another function, adding behavior without modifying the original code. They're used for caching, logging, or access control. The main footgun is forgetting that decorators run at definition time, not call time.

Why it exists

Python needed a clean, reusable way to add functionality to functions or methods without permanently altering their source code. Before decorators, this often involved manually passing functions to other functions, which was verbose and less readable. Decorators provide syntactic sugar (@my_decorator) for this common pattern of wrapping functions.

The mental model

Think of a decorator as a gift wrapper. It's a function that you 'wrap' around another function (the gift). When you use the gift, you're actually interacting with the wrapper first. The wrapper can do things before the original function runs (like log the arguments), run the original function, do things after it runs (like cache the result), and then return the result. The original function remains unchanged inside.

How it works

A decorator is a callable that returns a callable. When you write @my_decorator above def my_func():, Python translates this to my_func = my_decorator(my_func). The my_decorator function receives my_func as an argument. Inside my_decorator, you typically define a new inner function (often called wrapper) that contains the new logic plus a call to the original function. The decorator then returns this wrapper function, which replaces the original my_func in the surrounding scope.

When to use it

Use decorators for cross-cutting concerns that apply to many functions. Common use cases include: first, logging function entry/exit and arguments; second, caching results to avoid re-computation (like functools.lru_cache); third, enforcing access control or authentication in web frameworks like Flask or FastAPI; and fourth, timing function execution.

When not to use it

Don't use a decorator if the logic is specific to only one function; in that case, just put the logic inside the function itself. Avoid stacking too many decorators on a single function, as it can make debugging difficult by obscuring the call stack and making it hard to trace which decorator is doing what.

One canonical example

A classic example is a timing decorator. You'd define a function timer that accepts another function, func, as its argument. Inside timer, you define a wrapper function. This wrapper records the start time, calls the original func, records the end time, prints the duration, and then returns the original function's result. The timer function returns this wrapper. Applying @timer to a function waste_some_time means that whenever waste_some_time is called, it's the wrapper that actually runs, automatically timing the execution. A key detail is using @functools.wraps(func) inside the decorator to preserve the original function's name and docstring.

Interview question

When a decorator is applied to a function, what is the immediate effect on that function's definition?

  • a.It modifies the original function's source code to embed new functionality.
  • b.It passes the original function as an argument to the decorator each time the function is called.
  • c.It creates a separate, independent function that is called before the original function.
  • d.It replaces the original function in its scope with a new, wrapped function.Correct
Why?

The card states that the decorator returns a wrapper function, which 'replaces the original my_func in the surrounding scope.' This means the original function's name now refers to the new, wrapped function. Option A is incorrect because decorators add behavior without altering the original function's source code.

Just read this? Test yourself on what you have been reading.

Read the original → docs.python.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles