装饰器
定义:装饰器:本质是函数(装饰其他函数),为其他函数添加附加功能
原则: 1、不能修改被装饰的函数的源代码;
2、不能修改被装饰的函数的调用方式;
实现装饰器知识储备:
1、函数即“变量”
2、高阶函数
3、嵌套函数
高阶函数+嵌套函数=》 装饰器
1 import time 2 3 def timmer(func): 4 def warpper(*args,**kwargs): 5 start_time=time.time() 6 func() 7 stop_time=time.time() 8 print('the func run time is %s'%(stop_time-start_time)) 9 return warpper 10 11 @timmer 12 def test1(): 13 time.sleep(3) 14 print('in the test1') 15 16 test1()
装饰器之函数即变量:
1 def foo(): 2 print('in the foo') 3 bar() 4 foo() 5 6 def foo(): 7 print('in the foo') 8 bar() 9 def bar(): 10 print("100") 11 foo() 12 13 def foo(): 14 print('in the foo') 15 bar() 16 foo() 17 def bar(): 18 print("100")
装饰器之嵌套函数:
1 # 嵌套函数:在一个函数内再申明一个函数(def) 2 3 def foo(): 4 print("in the foo") 5 def bar(): 6 print("in the bar") 7 bar() 8 foo()
装饰器之高阶函数:
1 高阶函数:1、把一个函数名当做实参传给另一个函数(在不修改被装饰函数源代码的情况下,为其添加功能) 2 2、返回值中包含函数名(不修改函数的调用方式) 3 定义1情况 4 import time 5 def bar(): 6 time.sleep(3) 7 print('in the bar') 8 9 def test1(func): 10 start_time=time.time() 11 print(func) 12 func() #run bar()函数 13 stop_time=time.time() 14 print('the func run time is %s'%(stop_time-start_time)) 15 test1(bar) 16 17 定义2情况 18 import time 19 def bar(): 20 time.sleep(3) 21 print("in the bar") 22 def test2(func): 23 print(func) 24 return func 25 26 print(test2(bar)()) 27 t = test2(bar) 28 t() 29 print(t()) 30 bar = test2(bar) 31 bar()
装饰器之“奇迹版”:
1 import time 2 user,passwd="fs","123456" 3 def auth(auth_type): 4 print("auch func: ",auth_type) 5 def outer_wrapper(func): 6 def wrapper(*args,**kwargs): 7 print(" func : ",*args,**kwargs) 8 if auth_type == "local": 9 username = input("Username:").strip() # strip()去除首尾的空格和或数字0 10 password = input("Password:").strip() 11 12 if user == username and passwd == password: 13 print("\033[32;1mUser has passed authentication\033[0m") 14 res = func(*args,**kwargs) 15 print("-------->>>>>") 16 return res 17 18 else: 19 exit("\033[33;1mIMINVALID username or password\033[0m") 20 elif auth_type == "ldap": 21 print("远程服务器。。。。。。。。") 22 return wrapper 23 return outer_wrapper 24 25 26 def index(): 27 print("welcome to index page") 28 29 @auth(auth_type="local") 30 def home(): 31 print("welcome to home page") 32 return "fs from in the chain" 33 @auth(auth_type="ldap") 34 def bbs(): 35 print("welcome to bbs page") 36 index() 37 print(home()) 38 bbs()
装饰器案例实操1:
1 import time 2 def timer(func): #timer(test1) func=test1 3 def deco(): 4 start_time=time.time() 5 func() #run test1() 6 stop_time=time.time() 7 print("the func run time is %s"%(stop_time-start_time)) 8 return deco 9 @timer #就等于 test1 = timer(test1) 10 def test1(): 11 time.sleep(3) 12 print("in the test1") 13 @timer 14 def test2(): 15 time.sleep(3) 16 print("in the test2") 17 18 #test1=timer(test1) 19 test1() #--->run deco 20 test2()
装饰器案例实操2:
1 #带参数的装饰器调用 2 import time 3 def timer(func): #timer(test1) func=test1 4 def deco(*args,**kwargs): 5 start_time=time.time() 6 func(*args,**kwargs) #run test1() 7 stop_time=time.time() 8 print("the func run time is %s"%(stop_time-start_time)) 9 return deco 10 11 @timer #就等于 test1 = timer(test1) 12 def test1(): 13 time.sleep(3) 14 print("in the test1") 15 16 @timer 17 def test2(name,age): 18 time.sleep(3) 19 print("in the test2:",name,age) 20 21 #test1=timer(test1) 22 test1() #--->run deco 23 test2("fs",20)