Skip to main content

How to Use a Decorator in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A decorator is a function that wraps another function to change what it does, without touching its code. You put it on top of a function with @, as in @model_validator in pydantic or @classmethod. This guide writes a small decorator and shows what @ really does, on Python 3.15.2.

Syntax​

Syntax
def <decorator>(function):
def wrapper(<parameters>):
<do something with> function(<parameters>)
return wrapper


@<decorator>
def <name>(<parameters>):
...
PartWhat it means
<decorator>(function)A function that takes another function as its argument
wrapperThe new function the decorator returns. It usually calls the original one
@<decorator> above defReplaces the function with whatever the decorator returns

The Python glossary defines a decorator as "A function returning another function, usually applied as a function transformation using the @wrapper syntax."

The Example​

A greet function returns a greeting. A shout decorator wraps it so the greeting comes back in capital letters.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

shout.py
def shout(function):
def wrapper(name):
return function(name).upper()

return wrapper


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


print(greet("Ada"))

The shout.py script in a macOS-style code window: a shout decorator with an inner wrapper that upper-cases the result, a greet function decorated with @shout, and print(greet("Ada")).

Step 2: Run It​

Terminal
uv run python shout.py
Output
HELLO, ADA

The terminal after uv run python shout.py: HELLO, ADA.

greet("Ada") now runs wrapper, which calls the original greet and turns its result into capitals. The command works unchanged in PowerShell.

What @ Really Does​

@shout is a shorter way to write one line after the function:

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


greet = shout(greet)

That version printed the same HELLO, ADA. The language reference says the decorator "is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object."

Common Mistakes​

MistakeWhat happensDo this instead
Adding brackets to a decorator that takes none: @shout()TypeError: shout() missing 1 required positional argument: 'function', raised as soon as Python reads the definition: "Decorator expressions are evaluated when the function is defined"Write @shout without brackets
Forgetting return wrapperThe decorator returns None, so greet becomes None, and calling it fails: TypeError: 'NoneType' object is not callableEnd the decorator with return wrapper

Troubleshooting​

SymptomLikely causeFix
TypeError: ... missing 1 required positional argument pointing at the @ lineThe decorator was called with brackets, so it ran before it got the functionRemove the brackets, or use a decorator built to take arguments

FAQ​

How does a decorator with arguments, such as @model_validator(mode="after"), work?​

The part in brackets runs first and returns the real decorator, which then wraps the function. The language reference gives the rule: @f1(arg) above @f2 above def func(): pass "is roughly equivalent to" func = f1(arg)(f2(func)).

Can a function have more than one decorator?​

Yes. They're applied from the bottom up: the one nearest def wraps the function first, as the reference's f1(arg)(f2(func)) shows.

Conclusion​

@shout wrapped greet so that it returned HELLO, ADA, and it did exactly what greet = shout(greet) does. A decorator is a function that takes a function and returns a new one.

Additional Resources​