python二模块之装饰器
# 装饰器使用小高潮
import time
def timr(func): # timr(func)=timr(test1) func=test1
def deco(*args):
start_time = time.time()
func(*args) # run test1()
stop_time = time.time()
print("the func run time is {}".format(stop_time - start_time))
return deco
@timr # test1 = timr(test1)
def test1():
time.sleep(2)
print("in the test1")
@timr
def test2(name,age):
time.sleep(3)
print("test2 name is {},age is {}".format(name,age))
test1()
test2("hujinali", 21)
一路荒凉、一路彷徨,凉有多凉,徨有多徨

浙公网安备 33010602011771号