import timedef timer(func): # timer(tes1) func=tes1 def deco(*args,**kwargs): #传多少个参数都匹配 start_time=time.time() func(*args,**kwargs) #run tes1() #匹配deco传的参数 传多少个参数都匹配 stop_time=time.time() print("the func run time is %s" %(stop_time-start_time)) return deco @timer # tes1=timer(tes1)def tes1(): time.sleep(1) print('in the test1') @timerdef tes2(*args,**kwargs): time.sleep(1) print('test2:',args,kwargs) tes1() tes2("leiwenbin",22)
#高级版
import time user,password = 'alex','abc123' def auth(auth_type): print("auth func",auth_type) def outer_wrappaer(func): def wrapper(*args,**kwargs): print("auth func",*args,**kwargs) if auth_type == "local": username = input("Username:").strip() passwd = input("Password:").strip() if user == username and passwd == password: print("User has passwd authentication") res = func(*args, **kwargs) # from home print("----after authentication") return res else: exit("Invalid username or passoword") elif auth_type == "ldap": print("ldap验证...") return wrapper return outer_wrappaer def index(): print("welcome to index page") @auth(auth_type="local") # home=wrapper() def home(): print("welcome to home page") return "from home" @auth(auth_type="ldap") def bbs(): print("welcome to bbs page") index() print(home()) #wrappaer bbs()