How to Use a Decorator in Python
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
def <decorator>(function):
def wrapper(<parameters>):
<do something with> function(<parameters>)
return wrapper
@<decorator>
def <name>(<parameters>):
...
| Part | What it means |
|---|---|
<decorator>(function) | A function that takes another function as its argument |
wrapper | The new function the decorator returns. It usually calls the original one |
@<decorator> above def | Replaces 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
def shout(function):
def wrapper(name):
return function(name).upper()
return wrapper
@shout
def greet(name):
return f"Hello, {name}"
print(greet("Ada"))

Step 2: Run It
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:
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
| Mistake | What happens | Do 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 wrapper | The decorator returns None, so greet becomes None, and calling it fails: TypeError: 'NoneType' object is not callable | End the decorator with return wrapper |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: ... missing 1 required positional argument pointing at the @ line | The decorator was called with brackets, so it ran before it got the function | Remove 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
- decorator — the glossary entry.
- Function definitions — how Python applies decorators.
