from functools import wraps
def a_new_decorator(a_func):
@wraps(a_func)
def wrapTheFunction(a, b):
print("增加在函数运行前的限制或操作")
a_func(a, b)
print("增加在函数运行后的限制或操作")
return wrapTheFunction
@a_new_decorator
def a_function_requiring_decoration(a, b):
"""Hey yo! Decorate me!"""
print(a+b)
print("I am the function which needs some decoration to "
"remove my foul smell")
print(a_function_requiring_decoration.__name__)
# Output: a_function_requiring_decoration
a_function_requiring_decoration(1, 2)
Python之装饰器
最新推荐文章于 2024-07-12 19:06:42 发布