How to assure applying order of function decorators in Python?

Posted by Satoru.Logic on Stack Overflow See other posts from Stack Overflow or by Satoru.Logic
Published on 2012-09-04T02:58:48Z Indexed on 2012/09/04 3:38 UTC
Read the original article Hit count: 124

Some decorators should only be used in the outermost layer.

A decorator that augments the original function and add a configure parameter is one example.

from functools import wraps

def special_case(f):
    @wraps(f)
    def _(a, b, config_x=False):
        if config_x:
           print "Special case here"
           return
        return f(a, b)

How can I avoid decorators like this getting decorated by another decorator?

EDIT

It is really disgusting to let everyone trying to apply a new decorator worry about the application order.

So, is it possible to avoid this kind of situation? Is it possible to add a config option without introducing a new parameter?

© Stack Overflow or respective owner

Related posts about python

Related posts about decorator