d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

Utkarsh Shukla blogs

Python Decorators for Dummies: A Beginner’s Guide

Python decorators are a powerful and advanced feature that can enhance the functionality and behaviour of functions or methods in Python. However, they can also be confusing and intimidating for beginners. In this blog post, we will break down the concept of decorators in simple terms and provide coding examples to help you understand how they work.

What are Python Decorators?

In Python, a decorator is a higher-order function that takes a function as an argument and extends its behaviour without explicitly modifying the function’s code. In other words, decorators are used to add additional functionality to functions or methods, such as logging, timing, or authentication, without changing their implementation.

Decorators are commonly used to modify the behaviour of functions or methods that are repeatedly used in different parts of a codebase. They allow you to add or modify the behaviour of functions or methods by “decorating” them with additional code, which is executed before or after the original function or method is called.

Syntax of Python Decorators

The syntax for defining a decorator in Python involves using the “@” symbol followed by the name of the decorator function before the definition of the function or method that you want to decorate. Here’s an example:

def my_decorator(func):
    def wrapper(*args, **kwargs):
        # Additional code to be executed before the original function is called
        print("Decorator: Before function call")
        result = func(*args, **kwargs)
        # Additional code to be executed after the original function is called
        print("Decorator: After function call")
        return result
    return wrapper

@my_decorator
def my_function():
    print("Original function")

# Call the decorated function
my_function()

In the example above, my_decorator is a decorator function that takes a function func as an argument and defines a new function wrapper that wraps around the original function. The wrapper function is returned by the my_decorator function and is used to add additional code before and after the original function is called. The @my_decorator syntax is a shorthand for the following code:

def my_function():
    print("Original function")

# Call the decorated function
decorated_function = my_decorator(my_function)
decorated_function()

This makes the my_function function behave as if it has been decorated with the my_decorator function.

Examples of Python Decorators

Now, let’s look at some examples of Python decorators to illustrate their usage.

1. Logging Decorator

A logging decorator can be used to log information about when a function is called, what arguments are passed to it, and what it returns. Here’s an example:

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

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

result = add(2, 3)
print(f"Result: {result}")

Output:

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

In this example, the logging_decorator is used to log information about the add function, such as when it is called, what arguments are passed to it, and what it returns.

2. Timing Decorator

A timing decorator can be used to measure the time it takes for a function to execute. Here’s an example:

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: {func.__name__} took {end_time - start_time:.2f} seconds to execute")
        return result
    return wrapper

@timing_decorator
def slow_function():
    # Simulate a slow function
    time.sleep(2)
    print("Slow function executed")

slow_function()

Output:

Timing: slow_function took 2.00 seconds to execute
Slow function executed

In this example, the timing_decorator is used to measure the time it takes for the slow_function to execute, and the result is printed after the function is called.

3. Authentication Decorator

An authentication decorator can be used to add authentication logic to a function or method to ensure that it can only be executed by authenticated users. Here’s an example:

def authentication_decorator(func):
    def wrapper(*args, **kwargs):
        # Check if user is authenticated
        if is_authenticated():
            result = func(*args, **kwargs)
        else:
            print("Authentication: User is not authenticated")
            result = None
        return result
    return wrapper

@authentication_decorator
def protected_function():
    print("Protected function executed")

# Simulate an authenticated user
is_authenticated = True

protected_function()

Output:

Protected function executed

In this example, the authentication_decorator is used to check if the user is authenticated before allowing the protected_function to be executed. If the user is not authenticated, a message is printed and the function is not called.

Conclusion

Python decorators are a powerful feature that can be used to enhance the functionality of functions or methods in Python. They allow you to add or modify the behavior of functions or methods without changing their implementation, making them a versatile tool for code customization. In this blog post, we provided a beginner-friendly introduction to Python decorators with simple coding examples to help you understand their usage. We hope this guide helps you grasp the concept of decorators and inspires you to explore and use them in your Python projects. Happy coding!

References

Add Comment