from time import ctime, sleep
deftimefun(func):defwrapped_func():
print("%s called at %s" % (func.__name__, ctime()))
func()
return wrapped_func
@timefundeffoo():
print("I am foo")
foo()
sleep(2)
foo()
8.完成一个装饰器,函数有参数
from time import ctime, sleep
deftimefun(func):defwrapped_func(a, b):
print("%s called at %s" % (func.__name__, ctime()))
print(a, b)
func(a, b)
return wrapped_func
@timefundeffoo(a, b):
print(a+b)
foo(3,5)
sleep(2)
foo(2,4)
9.完成一个装饰器,函数有返回值
from time import ctime, sleep
deftimefun(func):defwrapped_func():
print("%s called at %s" % (func.__name__, ctime()))
return func()
return wrapped_func
@timefundeffoo():
print("I am foo")
@timefundefgetInfo():return'----hahah---'
foo()
sleep(2)
foo()
print(getInfo())