Concept of decorator & actual use of dry goods

Keywords: Python

Definition:
  • Function in essence (decorate other functions), is to add additional functions for other functions
Principle:
  • The source code of the decorated function cannot be modified
  • Cannot modify the call mode of decorated function
 
Realize knowledge reserve of decorators:
  • Functions and "variables"
  • Higher order function
  1. Pass a function name as an argument to another function (add function without modifying the decorated function)
  2. The return value contains the function name (without modifying the function call method). Use return to return the memory address
  • Nested function

Decorator = higher order function + nested function

Actual usage:

 1 #Decorator test
 2 
 3 import time
 4 
 5 #Decorator
 6 def adron(fun):
 7     print('Enter the memory address of the function to be decorated:%s'%fun)
 8     def ad_x(*args,**kwargs):
 9         start_time = time.time()
10         #fun+()To execute the decorated function
11         fun(*args,**kwargs)
12         stop_time = time.time()
13         print('visit world use time: %s'%(stop_time-start_time))
14     return ad_x
15 
16 #Decorated function
17 @adron # @adron = adron(tet),take tet The memory address is passed as a parameter to the decorator function
18 def tet():
19     time.sleep(3)
20     print('hello world!')
21 #Here tet When decorated by a decorator, the memory address is ad_x Memory address of
22 tet()
23 
24 @adron
25 def jsq(n,s):
26     for i in range(n):
27         time.sleep(s)
28         print(i)
29 
30 jsq(5,0.1)

High level decorator:

 1 #Big decorator
 2 
 3 name = 'sober'
 4 password = '123'
 5 
 6 def badn(action):
 7     print("Login method:%s"%action)
 8     #fun from @badn Decorator in
 9     def bt_badn(fun):
10         print('See fun Value:%s'%fun)
11         def adron(*args,**kwargs):
12             if action == 'local':
13                 name_value = input('Please user name:')
14                 password_value = input('Please Password:')
15                 if name == name_value and password == password_value:
16                     #fun The decorated function is executed because the fun Is the memory address of the function being executed
17                     ret_value = fun(*args,**kwargs)
18                     print('After the decorated function is executed in the decorator, it returns to the front')
19                     #Returns the result after the decorated function is executed
20                     return ret_value
21                 else:
22                     print('username or password error')
23             elif action == 'ldap':
24                 print('ldap is unrealized')
25         #return Return function name is to return function memory address and use memory address+()Method of calling function
26         return adron
27     return  bt_badn
28 
29 
30 def index():
31     print('welcome !hello world!')
32 
33 @badn(action='local') # @badn = badn(home) If you want to pass in parameters, you need to nest another layer within the decorator function
34 def home():
35     print('welcome home path')
36     return 'a'
37 
38 @badn(action='ldap')
39 def ldap():
40     print('welcome ldap enter')
41 
42 index()
43 #The execution here passes through the function memory address+()Execution function
44 #home()
45 print('display home Return value:%s'%home())
46 ldap()

Note: learn oldboy python automatic operation and maintenance -- Notes on decorator

The code adds my understanding of decoration use

Posted by silvermice on Sun, 05 Apr 2020 07:18:31 -0700