HOWTO · Python

How to Implement Multiple Decorators in Python

Learn how Python decorators wrap functions, how @ syntax applies a decorator, and how stacked decorators are applied and called.

On this page

Python decorators let you add behavior around a function while keeping that behavior separate from the function’s main task. A decorator is a callable that receives a function and returns a replacement, often a wrapper function. Applying more than one decorator lets you compose these behaviors.

How decorators wrap a function

Functions are first-class objects in Python: you can assign them to variables, pass them as arguments, and return them from other functions. They are not iterable objects. A decorator uses this callable behavior to receive a function and provide another callable in its place.

The original function is often called the wrapped function. A wrapper can do work before or after calling it, and it can pass arguments through and return the wrapped function’s result. If a wrapper does not forward arguments or the return value, it changes the interface or behavior of the original function, so include that forwarding when the decorated function needs it.

Build a decorator that wraps a function

This decorator prints a message before and after the wrapped call. *args and **kwargs forward positional and keyword arguments, while return preserves the wrapped function’s result. This is a basic decorator that accepts a function; it is not a decorator factory that accepts configuration.

def announce(func):
    def wrapper(*args, **kwargs):
        print("Starting")
        result = func(*args, **kwargs)
        print("Finished")
        return result

    return wrapper


def greet(name):
    return f"Hello, {name}!"


greet = announce(greet)
print(greet("Ada"))
Starting
Finished
Hello, Ada!

The supplied runtime evidence records this output. When called, announce returns wrapper; that wrapper calls the original greet with its argument, then returns the greeting. Finished is printed when the wrapped call returns normally. With this wrapper, if func raises an exception, execution skips that print and the exception propagates because the call is not protected by try/finally. In production decorators, functools.wraps(func) is commonly applied to the wrapper to preserve useful metadata such as the original function’s name and docstring.

Apply a decorator with @

The @ form applies a decorator when Python executes a function definition. The expression @announce immediately above def greet(...): is equivalent to defining the function and then assigning greet = announce(greet). The name @function_name describes the general syntax: replace function_name with the decorator’s name.

This happens as the definition is executed, typically while the module is imported. Later calls use the returned callable bound to the function’s name. A decorator that takes a function directly, such as announce, differs from a decorator factory: a factory first accepts configuration and returns a decorator.

Stack multiple decorators

Place decorators on consecutive lines immediately above the function. Python applies them from the line nearest the function upward. Thus @outer above @inner is equivalent to target = outer(inner(target)). When the resulting function is called, calls enter the outer wrapper first; if each wrapper calls the callable it received, execution then reaches the inner wrapper and finally the original function. When each wrapped call returns normally, their after-call work unwinds in reverse order. With these wrappers, if a wrapped call raises an exception, the following after-call print is skipped and the exception propagates because the call is not protected by try/finally.

The example uses two decorators to make this order visible. The inner decorator surrounds the function first, then outer surrounds that result. The supplied runtime evidence records the output below.

def inner(func):
    def wrapper():
        print("Inner before")
        result = func()
        print("Inner after")
        return result

    return wrapper


def outer(func):
    def wrapper():
        print("Outer before")
        result = func()
        print("Outer after")
        return result

    return wrapper


@outer
@inner
def target():
    print("Target")
    return "done"


result = target()
print(result)
Outer before
Inner before
Target
Inner after
Outer after
done

If a wrapper needs parameters, forward them to the callable it receives, as in the earlier *args, **kwargs example. For decorators that should work with functions or methods and retain metadata, use functools.wraps; for asynchronous functions, the wrapper generally needs to be async and await the wrapped call. These choices preserve the behavior the decorated function’s callers expect.