Python Decorators

Carson West

Python Functions

Python Decorators

Python decorators are a powerful and expressive feature that allows you to modify or enhance functions and methods in a clean and readable way. They use the @ symbol followed by the decorator function name, placed above the function definition.

Basic Syntax:

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()

This code shows a simple decorator. my_decorator takes a function (func) as input, creates a wrapper function that executes code before and after func, and returns the wrapper. The @my_decorator syntax is equivalent to say_hello = my_decorator(say_hello).

Decorator with Arguments: Decorators can also accept arguments. This requires a bit more complexity:

def repeat(num_times):
    def decorator_repeat(func):
        def wrapper(*args, **kwargs):
            for _ in range(num_times):
                result = func(*args, **kwargs)
            return result
        return wrapper
    return decorator_repeat

@repeat(num_times=3)
def greet(name):
    print(f"Hello, {name}!")

greet("World")

Here, repeat is a decorator factory – it returns a decorator. The inner function decorator_repeat takes the function to be decorated, and the innermost wrapper handles the actual repetition.

Use Cases:

Advanced Topics:

Further Reading:

Related Notes: