A decorator is a function that wraps another function to add extra behavior, without permanently modifying the original function’s code — applied using the @decorator_name syntax above a function definition.
Decorators let you add functionality — like logging, timing, or access checks — around an existing function in a clean, reusable way.
def my_decorator(func):
def wrapper():
print("Before the function runs")
func()
print("After the function runs")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
Calling say_hello() now runs the wrapper function, which prints a message, runs the original function, and prints another message — all without changing the original say_hello function's code.
Last reviewed: September 2026