Decorators.md

November 28, 2023 · View on GitHub

Advanced Python Concepts: Decorators

Decorators in Python are a very powerful and useful tool that allows programmers to modify the behavior of a function or class. Decorators allow you to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it.

Basic Concept

In Python, functions are first-class objects, meaning they can be passed around and used as arguments. A decorator is a function that takes another function and extends its behavior without explicitly modifying it.

Defining a Decorator

A decorator is defined as a function that takes another function as an argument and returns yet another function.

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

def say_hello():
    print("Hello!")

# Applying the decorator
say_hello = my_decorator(say_hello)

When you call say_hello(), the wrapper function is called, which adds behavior before and after the say_hello function runs.

Using the @ Syntax for Decorators

Python allows a simpler syntax for applying decorators using the @ symbol.

@my_decorator
def say_hello():
    print("Hello!")

say_hello()

Decorators with Parameters

Sometimes, you might need a decorator that accepts arguments. You can achieve this by adding another level of function nesting.

def decorator_with_args(arg):
    def my_decorator(func):
        def wrapper():
            print(f"Decorator argument: {arg}")
            func()
        return wrapper
    return my_decorator

@decorator_with_args("Custom argument")
def say_hello():
    print("Hello!")

say_hello()

Built-in Decorators: @staticmethod and @classmethod

Python provides a couple of built-in decorators: @staticmethod and @classmethod.

  • @staticmethod: Defines a method as a static method that doesn’t access instance or class data.
  • @classmethod: Defines a method as a class method that receives the class as the first argument.

Conclusion

Decorators are a very powerful and expressive feature of Python. They allow for the extension or alteration of a function's behavior in a clean, concise, and readable manner. This capability is extremely useful for aspects like logging, access control, measuring execution times, and more, without having to change the function's code.