Decorator's @method usage

#Decorator shelf # 
@timer   is equivalent to test=timer (test0 
import time
 def timer(func):
     def wrapper():
        starttime=time.time()
        func() # is running test() 
        stoptime= time.time()
         print ( ' running time %s ' %(stoptime- starttime))
         # print(func) 
    return wrapper
 @timer #equivalent to test=timer(test) , a decorator name is enough, you can directly call 
def test():
    time.sleep( 3 )
     print ( ' The test function finished running ' )

res =timer(test) #wrapper () address when returned 
res() #wrapper () when executed

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325338101&siteId=291194637