How to use Python Decorators_0
- Authorization
- Logging
通过装饰器可以来打印日志:
from functools import wraps
def logit(func):
@wraps(func)
def with_logging(*args, **kwargs):
print(func.__name__ + " was called")
return func(*args, **kwargs)
return with_logging
@logit
def addition_func(x):
"""Do some math."""
return x + x
result = addition_func(4)
# Output: addition_func was called
最后的回显是:
addition_func was called
需要注意下Decorators的return顺序;
Decorators自身不会Return;
最后Return的是Function_Operation
最先Return的是Function