Decorators commonly used in python

# 1. Add a decorator for counting time to the function: 
import time
 def timer(func):
     def inner():
        starttime=time.time()
        print(starttime)
        func()
        stoptime=time.time()
        print(stoptime)
        use_time=stoptime-starttime
        print(use_time)
    return inner

@timeer
def test():
    time.sleep(10)
    print("test")

test()

# 2. Add an authentication decorator to the function: 
def auth(func):
    
    def inner()
        
        func()
    return inner

 

Guess you like

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