Can I combine two decorators into a single one in Python?
Solution 1:
A bit more general:
def composed(*decs):
def deco(f):
for dec in reversed(decs):
f = dec(f)
return f
return deco
Then
@composed(dec1, dec2)
def some(f):
pass
is equivalent to
@dec1
@dec2
def some(f):
pass
Solution 2:
Yes. See the definition of a decorator, here.
Something like this should work:
def multiple_decorators(func):
return decorator1(decorator2(func))
@multiple_decorators
def foo(): pass