Simple:
def both_decorators(func):
return decorator1(decorator2(func))
because that's all decorators do, really.
Frage
Say, I have something like:
@decorator1
@decorator2
def myfunc()
# ....
How do I declare a new decorator both_decorators
which would invoke decorator1
and decorator2
in order, essentially making it an alias for them? So that I could write instead:
@both_decorators
def myfunc()
# ....
The idea is to save typing when multiple decorators are used the same way in many cases.
Lösung
Simple:
def both_decorators(func):
return decorator1(decorator2(func))
because that's all decorators do, really.
Andere Tipps
Yes, you can. Something along the lines of:
def both_decorators(*decs):
def decorator(dc):
for dec in reversed(decs):
dc = dec(dc)
return dc
return decorator
Then all you would need to do is add them
@both_decorators(decorator1, decorator2)
def myfunc():
#something happening here.
Personally I would prefer this, as you can choose which decorator you would want and which not.