普通装饰器:用于查看函数运行时间
def use_time(fun): def inner(*args,**kwargs): start_time = time.time() result = fun() stop_time = time.time() print("任务执行使用时间%s秒" % (stop_time-start_time)) return result return inner @use_time def fun(): for i in range(1000000): pass return "success"
复杂装饰器:装饰器带参数
import time user,passwd = 'aaa','123' def auth(auth_type): print("auth func:",auth_type) def outer_wrapper(func): def wrapper(*args, **kwargs): print("wrapper func args:", *args, **kwargs) if auth_type == "local": username = input("Username:").strip() password = input("Password:").strip() if user == username and passwd == password: print("