import time def timeer(func): def wrapper(*args,**kwargs): start_time=time.time() func() end_time=time.time() res=end_time-start_time return res #返回值 return wrapper @timeer #foo=timeer(foo)=wrapper def foo(): time.sleep(3) print("in the foo") res=foo() #foo()==wrapper(),原来的foo没有返回值,现在有了 print(res)
in the foo
3.0
import time
def Time(check='file'):
def timeer(func):
def wrapper(*args,**kwargs):
print("check",check) #参数传递进去了,目的达到
start_time=time.time()
func(1,2,3)
end_time=time.time()
res=end_time-start_time
return res
return wrapper
return timeer
@Time(check='file') #带参数的修饰器 1.先执行Time,返回timmer 2.执行@timmer,即 foo=timmer(foo),返回wrapper,即foo=wrapper
def foo(*args,**kwargs):
time.sleep(3)
print("in the foo")
res=foo() #3.等于在执行wrapper()
print(res)
check file
in the foo
3.0