Python学习之路:装饰器实现,,import ti


import  timedef timer(func):#timer(test1) func=test1    def deco():        start_time=time.time()        func()#run test1        stop_time=time.time()        print(‘the func run time is %s‘%(stop_time-start_time))    return decodef test1():    time.sleep(3)    print(‘in the test1‘)def test2():    time.sleep(3)    print(‘in the test2‘)print(timer(test1))test1=timer(test1)test1()#----->deco#-------------------------------------------------------------------import  timedef timer(func):#timer(test1) func=test1    def deco():        start_time=time.time()        func()#run test1        stop_time=time.time()        print(‘the func run time is %s‘%(stop_time-start_time))    return deco@timer #加装饰器 test1=timer(test1)def test1():    time.sleep(3)    print(‘in the test1‘)@timer #加装饰器def test2():    time.sleep(3)    print(‘in the test2‘)test1()test2()

Python学习之路:装饰器实现

评论关闭