Post

The Debug Decorator

A decorator is a function or a class that wraps (or decorates) a function or a method, modifying their coded behaviour.

A simple definition:

Decorator is a design pattern that attaches additional responsibilities to an object dynamically.1

Decorators are useful for separating external logic from polluting the core logic of the function.2

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import functools

def debug(func):
    """Print the function signature and return value"""
    @functools.wraps(func)
    def wrapper_debug(*args, **kwargs):
        args_repr = [repr(a) for a in args]                      # 1
        kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()]  # 2
        signature = ", ".join(args_repr + kwargs_repr)           # 3
        print(f"Calling {func.__name__}({signature})")
        value = func(*args, **kwargs)
        print(f"{func.__name__!r} returned {value!r}")           # 4
        return value
    return wrapper_debug

Decorators are a powerhouse when it comes to reusing functionality across multiple objects. The above debug decorator can wrap around all functions and methods in your project to make debugging a cakewalk.

If you’re confused about the wrapper function nested within the decorator, here’s a thread that might help.3

Final Thoughts

Decorators have more fancy usecases to be explored, hopefully in a future post. Meanwhile, here’s a video introduction to the concept.4

Resources

This post is licensed under CC BY 4.0 by the author.