函数装饰器

装饰器:    高阶函数+嵌套函数 = 装饰器

定义:

本质是函数,装饰其它函数就是为其他函数添加附加功能

原则:
  1. 不能修改被修饰的函数源代码
  2.  不能修改被装饰的函数的调用方式
实现装饰器知识储备:
  1. 函数即“变量”
  2. 高阶函数:
    • 把一个函数名当做实参传给另一个函数(在不修改本装饰函数源代码的情况下为其添加功能)
    • 返回值中包含函数名(不修改函数的调用方式)
  3. 嵌套函数 

 

这是几个传递的方式:

1 bar() #这个代表函数,bar代表的是内存地址。
2 Test(bar)#调用bar的内存地址
3 Test(bar())#调用的是bar的返回值

 

装饰器案例:

   案例一:无参数传递
 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 
10 @timer  # test1=timer(test1),把deco的地址给test1,这个test1不是原来的test1了现在是包含其他功能的deco
11 def test1():
12     time.sleep(1)
13     print('in the test1')
14 
15 test1()

   结果:

1 in the test1
2 the func run time  is 1.0000569820404053

 

 案例二:有参数传递
 1 import time
 2 def timer(func): #timer(test2)  func=test2
 3     def deco(*args,**kwargs):
 4         start_time=time.time()
 5         func(*args,**kwargs)   #run test2()
 6         stop_time = time.time()
 7         print("the func run time  is %s" %(stop_time-start_time))
 8     return deco
 9 
10 
11 @timer  # test2=timer(test2) = deco  test2() =deco()
12 def test2(name,age) :
13      print('test2 :', name,age)
14 
15 
16 test2('tiger',19)

  结果:

1 test2 : tiger 19
2 the func run time  is 0.0

 

 案例综合:
 1 import time
 2 def timer(func): #timer(test1)  func=test1
 3     def deco(*args,**kwargs):
 4         start_time=time.time()
 5         func(*args,**kwargs)   #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),把deco的地址给test1,这个test1不是原来的test1了现在是包含其他功能的deco
10 def test1():
11     time.sleep(1)
12     print('in the test1')
13 
14 @timer  # test2=timer(test2) = deco  test2() =deco()
15 def test2(name,age) :
16     print('test2 :', name,age)
17 
18 test1()
19 test2('tiger',19)

 

案例高级迭代:

主要是   @auth(auth_type="local") 的应用

 1 import time
 2 user,passwd = 'tiger','abc123'
 3 def auth(auth_type):
 4     print("auth func:",auth_type)
 5     def outer_wrapper(func):
 6         def wrapper(*args, **kwargs):
 7             print("wrapper func args:", *args, **kwargs)
 8             if auth_type == "local":
 9                 username = input("Username:").strip()
10                 password = input("Password:").strip()
11                 if user == username and passwd == password:
12                     print("\033[32;1mUser has passed authentication\033[0m")
13                     res = func(*args, **kwargs)  # from home
14                     print("---after authenticaion ")
15                     return res
16                 else:
17                     exit("\033[31;1mInvalid username or password\033[0m")
18             elif auth_type == "ldap":
19                  print("搞毛线ldap,不会。。。。")
20 
21         return wrapper
22     return outer_wrapper
23 
24 def index():
25      print("welcome to index page")
26 @auth(auth_type="local") # home = wrapper() 因为加括号了
27 def home():
28     print("welcome to home  page")
29     return "from home"
30 
31 @auth(auth_type="ldap")
32 def bbs():
33      print("welcome to bbs  page")
34 
35 index()
36 home() #wrapper()
37 bbs()

 

posted @ 2020-02-19 21:28  穆帅虎  阅读(166)  评论(0)    收藏  举报