1 min read

Decorators in Python

Decorators in Python
Photo by Zach Graves / Unsplash

Decorators are a powerful feature in Python that allow you to modify the behavior of functions or classes without directly changing their source code. They provide a way to add functionality to existing functions or classes, such as logging, timing, authentication, or error handling.  

How Decorators Work

A decorator is essentially a function that takes another function as input and returns a modified version of that function. Here's a basic example:  

def my_decorator(func):
    def wrapper():
        print("Before function call")
        func()
        print("After function call")
    return wrapper

@my_decorator
def my_function():
    print("Inside    the function")

my_function()

Output:

Before function call
Inside the function
After function call

In this example:

  1. my_decorator is a decorator function.
  2. It takes my_function as input.
  3. It defines a wrapper function that prints messages before and after the my_function call.
  4. The @my_decorator syntax is syntactic sugar that applies the decorator to my_function.

Common Use Cases

Decorators are widely used in various scenarios:

  • Logging: Logging function calls and their arguments, return values, and execution time.
  • Timing: Measuring the execution time of functions.
  • Authentication: Checking user credentials before allowing access to a function.
  • Error Handling: Implementing custom error handling mechanisms.
  • Caching: Caching function results to improve performance.
  • Rate Limiting: Limiting the frequency of function calls.

Creating Custom Decorators

You can create your own decorators to tailor them to specific needs. Here's a simple example of a decorator that measures function execution time:

import time

def timer(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"Function    {func.__name__} took {end_time - start_time:.2f} seconds to execute.")
        return result
    return wrapper   

Key Points to Remember:

  • Decorators are a powerful tool for extending function and class behavior.
  • They can be chained to apply multiple modifications to a single function.
  • Be mindful of how decorators affect function arguments and return values.
  • Use decorators judiciously to avoid overly complex code.

By effectively using decorators, you can write cleaner, more efficient, and more maintainable Python code.