Demystifying Decorators in Python: A Powerful Tool for Function Manipulation

Ahtesham ZaidiAhtesham Zaidi
2 min read

Introduction
Decorators are a powerful feature in Python that allows you to modify function behavior at runtime. They are widely used in Python programming but can be a bit confusing for newcomers. In this article, we'll dive into the world of decorators, demystifying their concepts and exploring how they can be used to manipulate functions in Python.

1. Understanding Decorators:

Decorators in Python are functions that can be used to modify the behavior of other functions. They are applied to functions using the "@" symbol followed by the name of the decorator. Let's take a look at a simple example of a decorator that adds logging functionality to a function:

def logger(func):
    def wrapper(*args, **kwargs):
        print(f"Logging: Calling {func.__name__} with args {args} and kwargs {kwargs}")
        return func(*args, **kwargs)
    return wrapper

@logger
def greet(name):
    print(f"Hello, {name}!")

greet("Alice")

Output

Logging: Calling greet with args ('Alice',) and kwargs {}
Hello, Alice!

2. Decorator Examples:

Decorators can be used for various purposes, such as logging, timing, authentication, and caching. Here's an example of a timing decorator that measures the execution time of a function:

import time

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

@timing_decorator
def fib(n):
    if n <= 2:
        return 1
    a,b = 1,1
    for i in range(2,n):
        c = a + b
        a,b = b,c
    return c

print(fib(9))

Output

Timing: function fib took 0.000000 seconds to execute
34

3. Creating Your Own Decorators:

You can also create your own decorators in Python. Here's an example of a custom decorator that adds authorization functionality to a function:

def authorization_decorator(func):
    def wrapper(*args, **kwargs):
        if is_user_authorized():
            return func(*args, **kwargs)
        else:
            print("Authorization failed. Access denied.")
    return wrapper

@authorization_decorator
def sensitive_operation():
    print("Performing sensitive operation...")

sensitive_operation()

4. Advanced Decorator Techniques:

You can use advanced techniques with decorators, such as using class-based decorators, applying decorators to classes and methods, and chaining multiple decorators together. Here's an example of using a class-based decorator to measure the execution time of methods in a class:

import time

class TimingDecorator:
    def __init__(self, func):
        self.func = func

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

class MyClass:
    @TimingDecorator
    def my_method(self):
        print("Performing my_method...")

obj = MyClass()
obj.my_method()

Output

Performing my_method...
Timing: my_method took 0.00 seconds to execute

Have you used decorators in your Python projects? Share your experiences and thoughts in the comments below! If you have any questions or feedback, feel free to ask. Happy coding!

0
Subscribe to my newsletter

Read articles from Ahtesham Zaidi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ahtesham Zaidi
Ahtesham Zaidi

As a Software Engineer, I derive great pleasure from finding solutions to challenges and contributing to the improvement of our world.