Decorators are confusing and probably should be avoided till you are super experienced with python. That being said, chaining decorators is even more tricky:
from functools import wraps
def split(fn): # fn is the passed in function
@wraps(fn) # This means we can grabs its args and kwargs
def wrapped(*args, **kwargs): # This is the new function declaration
return fn(*args, **kwargs).split()
return wrapped
def uppercase(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
return fn(*args, **kwargs).upper()
return wrapped
# Order matters. You can't call .upper() on a list
@split
@uppercase
def CallFunction():
return "my string was in lower case"
res = CallFunction()
print(res)
Alternatively if you don't want the order of these two decorators to matter than you need to handle the list
case:
def uppercase(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
result = fn(*args, **kwargs)
if isinstance(result, list):
return [x.upper() for x in result]
return result.upper()
return wrapped
Reference: How to make a chain of function decorators?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…