logo

Decorators with Arguments

Most functions take arguments. Your decorator needs to pass them through.

def my_decorator(func):
    def wrapper(*args, **kwargs):
        print("Calling function")
        result = func(*args, **kwargs)
        print("Function returned")
        return result
    return wrapper

The *args, **kwargs pattern accepts any arguments and forwards them to the original function.

@my_decorator
def add(a, b):
    return a + b

result = add(2, 3)  # Prints messages, returns 5

Don't forget to return the result! Without return result, your decorated function would always return None.

This pattern works for any function signature - no arguments, many arguments, keyword arguments, defaults - everything passes through.

I explain argument handling in my Python Decorators course.