Skip to content

Python decorators

import functools

def do_twice(func):
    @functools.wraps(func)
    def wrapper_do_twice(*args, **kwargs):
        func(*args, **kwargs)
        return func(*args, **kwargs)
    return wrapper_do_twice

Order of decorators

@decorator2
@decorator1
def my_fn():
    pass

Expands outwards

decorator2(decorator1(my_fn()))


Last update: 2022-09-23