Python | Decorators

Python | Decorators

Python, a versatile and powerful programming language, is known for its simplicity and readability. One of the features that contribute to its simplicity is decorators. Decorators are a form of metaprogramming that allow you to modify the behavior of functions or methods in Python. They are a powerful tool for adding functionality, enhancing code readability, and maintaining clean and modular code. In this article, we will dive into the world of decorators in Python, exploring what they are, how they work, and practical use cases.

Understanding Decorators

At its core, a decorator is a higher-order function that takes another function as an argument and returns a new function that usually extends or modifies the behavior of the original function. Decorators are essentially wrappers around functions, allowing you to add functionality without modifying the original code. They are denoted by the “@” symbol followed by the decorator function’s name, placed just above the function definition.

syntax:

@decorator_function
def my_function():
    # Function code here

Here’s a basic example of a decorator in action:

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 BioChemiThon!")

say_hello()

Output:

Something is happening before the function is called.
Hello BioChemiThon!
Something is happening after the function is called.

In the above example, my_decorator takes say_hello as an argument, wraps it with additional functionality (printing messages before and after), and then returns the modified function. When we call say_hello(), it executes the wrapped version, which includes the decorator’s behavior.

Common Use Cases for Decorators

1) Logging: Decorators are commonly used for logging. You can create a logging decorator to record function calls, arguments, and return values. Here’s an example:

def log_function_call(func):
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
        print(f"{func.__name__} returned: {result}")
        return result
    return wrapper

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

add(2, 3)

Output:

Calling add with args: (2, 3), kwargs: {}
add returned: 5

The log_function_call decorator logs information about the function call.

2) Authorization: You can use decorators to control access to certain functions or routes in a web application. Here’s a simplified example:

def require_login(func):
    def wrapper(*args, **kwargs):
        if user_is_authenticated():
            return func(*args, **kwargs)
        else:
            return "Unauthorized"
    return wrapper

@require_login
def secret_page():
    return "This is a secret page!"

The require_login decorator ensures that only authenticated users can access the secret_page.

3) Caching: Decorators can be used to cache expensive function calls to improve performance. Here’s a simple caching decorator:

from functools import lru_cache

@lru_cache(maxsize=32)
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)
	

The @lru_cache decorator caches the results of the fibonacci() function, reducing redundant calculations.

4) Method Decorators: Decorators can also be applied to methods within classes. For instance, you can create a decorator to measure the execution time of methods:

import time

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

class MyClass:
    @timing_decorator
    def slow_method(self):
        time.sleep(2)

obj = MyClass()
obj.slow_method()

Output:

slow_method took 2.0090839862823486 seconds to execute

In the above example, the timing_decorator is applied to the slow_method() of the MyClass class.

Creating Your Own Decorators

To create custom decorators, you need to define a Higher Order Python function that takes a function as its argument, within this function you will create one more function typically(let’s say) named wrapper, and then within this wrapper function, you can manipulate the input function, extend its behavior, or perform any other desired actions. Here’s a simple example of a custom decorator that measures the execution time of a actual 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"{func.__name__} took {end_time - start_time} seconds to execute")
        return result
    return wrapper

@timing_decorator
def slow_function():
    time.sleep(2)

slow_function()

Output:

slow_function took 2.0099096298217773 seconds to execute

In the above example, the timing_decorator measures the execution time of slow_function() without modifying the original function’s code. Basically when we call the slow_function() then it executes the timing_decorator() higher order function which returns the wrapped version of the slow_function().

Conclusion

Decorators in Python are a powerful tool for enhancing the functionality and readability of your code. They allow you to separate concerns, add reusable functionality, and maintain clean and modular code. Whether you are a beginner or an experienced Python developer, understanding and using decorators can significantly improve your ability to write efficient and maintainable code.

Leave a Reply

Your email address will not be published. Required fields are marked *

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds