SyntaxStudy
Sign Up
Python Intermediate 4 min read

Decorator Basics

Decorator Basics

A decorator is a function that wraps another function to add behavior before or after it runs. Apply with @decorator_name syntax.

Example
def shout(func):
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        return result.upper()
    return wrapper

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

print(greet("world"))  # HELLO, WORLD
Pro Tip

@shout is shorthand for: greet = shout(greet)