函数对象在被调用时,函数装饰器将会起作用:
from time import ctime,sleep
def tsfunc(func):
def wrappedFunc():
print('[%s] %s() called'% (ctime(),func.__name__))
return func()
return wrappedFunc
@tsfunc
def foo():
pass
foo()
sleep(4)
for i in range(2):
sleep(1)
foo()
运行结果:
[Thu Feb 27 11:14:04 2020] foo() called
[Thu Feb 27 11:14:09 2020] foo() called
[Thu Feb 27 11:14:10 2020] foo() called
来源:CSDN
作者:零度香
链接:https://blog.csdn.net/u011837138/article/details/104532538