DEV Community

Cover image for Python Decorators: What is decorators and how to use them?
Devstories Playground
Devstories Playground

Posted on • Updated on

Python Decorators: What is decorators and how to use them?

Decorators are a powerful and versatile tool that allows you to modify the behavior of functions without permanently altering their original code. They work by wrapping a function in another function, which adds some extra functionality before or after the original function executes.

What they are:

Decorators are higher-order functions, meaning they take another function as an argument and return a new function.

The new function returned by the decorator typically acts as a wrapper around the original function, adding or modifying its behavior.

How they work:

  1. Defining the Decorator: You create a function that takes another function as its argument. This outer function is the decorator.
  2. Inner Functionality: Inside the decorator, you can define logic to be executed before or after the wrapped function.This might involve additional calculations, logging messages, or other actions.
  3. Returning a New Function: The decorator usually returns a new function object. This new function holds the wrapped function and any additional behavior you've specified.

Benefits of using decorators:

  1. Clean and Maintainable Code: By encapsulating modifications in decorators, you keep your original functions clean and focused on their core functionality.
  2. Code Reusability: Decorators allow you to create reusable patterns that can be applied to different functions throughout your codebase.
  3. Improved Readability: Well-defined decorators can make code easier to understand by clearly indicating the modifications being made to functions.

Common Use Cases:

  1. Authorization: Checking if a user has the necessary permissions before allowing them to call a function.
  2. Logging: Recording information about function calls, including arguments and return values.
  3. Error Handling: Implementing common error handling patterns to catch and manage exceptions.
  4. Performance Monitoring: Measuring the execution time of functions to identify performance bottlenecks.
  5. Caching: Storing the results of function calls to avoid redundant calculations.

Here's a Python example demonstrating a simple decorator that logs the execution time of a function:

import time

def timing_decorator(func):
  """Decorator that measures the execution time of a function."""
  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:.4f} seconds to execute.")
    return result
  return wrapper

@timing_decorator
def some_function(n):
  """Example function that does some work."""
  total = 0
  for i in range(n):
    total += i * i
  return total

# Call the function with decoration
result = some_function(10000)
print(f"Result: {result}")
Enter fullscreen mode Exit fullscreen mode

We define a decorator timing_decorator that takes a function func as an argument. Inside the decorator, we create an inner function wrapper that takes arguments similar to the original function. When wrapper is called, it records the start time using time.time(). The original function func is then called with its arguments (*args, **kwargs). After func finishes, the end time is recorded. The execution time is calculated and printed along with the function name. Finally, the result from func is returned. The @timing_decorator syntax is used to decorate the some_function. This essentially replaces some_functionwith the wrapped function created by the decorator.

When you run this code, it will print the execution time of some_function along with the result. This is a simple example, but decorators can be used to implement various functionalities based on your needs.

Let's wrap up things

By understanding decorators, you can enhance your Python code's flexibility, maintainability, and overall design.

HAPPY CODING 🚀

Image description

Top comments (0)