Skip to main content

Advanced Guide to Python Decorators | Expert Python Techniques

In Python, decorators are a powerful feature used to modify or enhance functions and classes. They are a key part of advanced metaprogramming and are frequently used in logging, authentication, caching, metrics, and tracing in production systems. This document provides an in-depth explanation of the syntax and all common use cases of Python decorators with professional examples aimed at deep learning engineers.

1. Basic Syntax of a Decorator

def my_decorator(func):
    def wrapper(*args, **kwargs):
        print("Before function call")
        result = func(*args, **kwargs)
        print("After function call")
        return result
    return wrapper

@my_decorator
def say_hello(name):
    print(f"Hello, {name}!")

say_hello("Alice")

@my_decorator is equivalent to writing say_hello = my_decorator(say_hello).

2. Stacking Multiple Decorators

def deco1(func):
    def wrapper(*args, **kwargs):
        print("deco1")
        return func(*args, **kwargs)
    return wrapper

def deco2(func):
    def wrapper(*args, **kwargs):
        print("deco2")
        return func(*args, **kwargs)
    return wrapper

@deco1
@deco2
def greet():
    print("Greetings")

greet()

This applies decorators from the bottom up: greet = deco1(deco2(greet)).

3. Parameterized Decorators

def repeat(n):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for _ in range(n):
                func(*args, **kwargs)
        return wrapper
    return decorator

@repeat(3)
def shout():
    print("Python!")

shout()

This is called a decorator factory that generates decorators dynamically.

4. Using functools.wraps

from functools import wraps

def log(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        print(f"Calling {func.__name__}")
        return func(*args, **kwargs)
    return wrapper

@log
def process():
    """Does something important"""
    print("Processing")

print(process.__name__)
print(process.__doc__)

@wraps preserves the metadata (name, docstring) of the original function.

5. Applying Decorators to Classes

def class_decorator(cls):
    cls.extra_attr = 'injected'
    return cls

@class_decorator
class MyClass:
    pass

print(MyClass.extra_attr)

Since classes are objects, decorators can wrap them too.

6. Decorating Methods Inside a Class

def log_method(func):
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        print(f"Method {func.__name__} called")
        return func(self, *args, **kwargs)
    return wrapper

class Logger:
    @log_method
    def run(self):
        print("Running")

Logger().run()

7. Using Decorators for Authentication

def require_admin(func):
    @wraps(func)
    def wrapper(user):
        if user != 'admin':
            raise PermissionError("Access denied")
        return func(user)
    return wrapper

@require_admin
def view_dashboard(user):
    print("Dashboard visible")

view_dashboard("admin")

8. Advanced Use: Class-Based Decorators

class CountCalls:
    def __init__(self, func):
        self.func = func
        self.calls = 0

    def __call__(self, *args, **kwargs):
        self.calls += 1
        print(f"Call {self.calls} to {self.func.__name__}")
        return self.func(*args, **kwargs)

@CountCalls
def hello():
    print("Hello")

hello()
hello()

Conclusion

Decorators are a vital tool for advanced Python developers. They increase code modularity, reusability, and readability. The provided examples demonstrate how decorators can be applied in real-world machine learning and deep learning projects, especially for logging, validation, caching, and profiling. functools.wraps is particularly essential to maintain proper function introspection.

References

Comments

Popular

Understanding SentencePiece: A Language-Independent Tokenizer for AI Engineers

In the realm of Natural Language Processing (NLP), tokenization plays a pivotal role in preparing text data for machine learning models. Traditional tokenization methods often rely on language-specific rules and pre-tokenized inputs, which can be limiting when dealing with diverse languages and scripts. Enter SentencePiece—a language-independent tokenizer and detokenizer designed to address these challenges and streamline the preprocessing pipeline for neural text processing systems. What is SentencePiece? SentencePiece is an open-source tokenizer and detokenizer developed by Google, tailored for neural-based text processing tasks such as Neural Machine Translation (NMT). Unlike conventional tokenizers that depend on whitespace and language-specific rules, SentencePiece treats the input text as a raw byte sequence, enabling it to process languages without explicit word boundaries, such as Japanese, Chinese, and Korean. This approach allows SentencePiece to train subword models di...

Mastering the Byte Pair Encoding (BPE) Tokenizer for NLP and LLMs

Byte Pair Encoding (BPE) is one of the most important and widely adopted subword tokenization algorithms in modern Natural Language Processing (NLP), especially in training Large Language Models (LLMs) like GPT. This guide provides a deep technical dive into how BPE works, compares it with other tokenizers like WordPiece and SentencePiece, and explains its practical implementation with Python code. This article is optimized for AI engineers building real-world models and systems. 1. What is Byte Pair Encoding? BPE was originally introduced as a data compression algorithm by Gage in 1994. It replaces the most frequent pair of bytes in a sequence with a single, unused byte. In 2015, Sennrich et al. adapted BPE for NLP to address the out-of-vocabulary (OOV) problem in neural machine translation. Instead of working with full words, BPE decomposes them into subword units that can be recombined to represent rare or unseen words. 2. Why Tokenization Matters in LLMs Tokenization is th...

Using Gemini API in LangChain: Step-by-Step Tutorial

What is LangChain and Why Use It? LangChain  is an open-source framework that simplifies the use of  Large Language Models (LLMs)  like OpenAI, Gemini (Google), and others by adding structure, tools, and memory to help build real-world applications such as chatbots, assistants, agents, or AI-enhanced software. Why Use LangChain for LLM Projects? Chainable Components : Easily build pipelines combining prompts, LLMs, tools, and memory. Multi-Model Support : Work with Gemini, OpenAI, Anthropic, Hugging Face, etc. Built-in Templates : Manage prompts more effectively. Supports Multi-Turn Chat : Manage complex interactions with memory and roles. Tool and API Integration : Let the model interact with external APIs or functions. Let's Walk Through the Code: Gemini + LangChain I will break the code into  4 main parts , each showcasing different features of LangChain and Gemini API. Part 1: Basic Gemini API Call Using LangChain import os from dotenv import load_dotenv load_dot...