Hi, I'm ThadeusB.

I code stuff. I raise bees. I game.

Multiple Decorators for python functions

A decorator is typically a function that takes a function as an argument and then will do something with that function before calling that function. The return value of this decorator replaces the original function definition. There are many tools to help with writing decorators such as functools.wraps and others which I won't go into in this posting.

When you stack multiple decorators onto a python function, there is an order that these decorators will get executed in, which is not mentioned in the documentation.

The decorators are executed from innermost to outermost. For example.

@i_get_called_last
@i_get_called_second
@i_get_called_first
def my_decorated_function():
    return 1+2

So when designing your decorators, make sure that the first and second called decorators return compatible functions that the decorators preceding it would be able to operate with.