- what is a decorator
- The knowledge points of decorators (functions are variables, higher-order functions, nested functions)
- Example of a decorator with no arguments
- Decorator example with parameters
- Operation
1. What is a decorator
Essentially, decorators are higher-order functions that return a function. A decorator is a function
Principles of decorators:
- Do not modify the source code of the decorated object
- Do not modify how the decorated object is called
Second, the knowledge points involved in the decorator
- function as variable
- Higher order functions
- nested functions
Functions are variables:
In python, a variable is first defined, allocated memory space, and then used.
Take x=1, this simple assignment statement as an example. First allocate a space in memory, x points to this memory space, and "1" is stored in this memory space
The function name is also a variable in essence, so after the def test() statement is executed, a space is allocated in the memory, test points to the memory, and the function body is stored in the memory.
Code example:
#Author:Yueru Sun #Functions are variables #Example one: def foo(): print('in the foo') foo() #Example 2: def bar(): print('in the bar') def foo(): print('in the foo') bar() foo() #Demo three: #When variables are used, they are defined first and then used def foo(): print('in the foo') bar() def bar(): print('in the bar') foo() #Demo four: # def foo(): # print('in the foo') # bar() #foo()# reports an error because bar is not defined yet # def bar(): # print('in the bar')
In particular, it should be noted that example 3 can be executed successfully, "example 3" = "example 2"
Because foo() is executed, foo() and bar() are def first, so when foo() is executed, foo() and bar() can be found
Example 4 is not executable. Because bar is not yet defined in memory before foo() executes, it cannot be found
Higher order functions:
A higher-order function is a function that is passed as a parameter or whose return value is a function
Nested functions:
def f1(): def f2(): def f3(): print('from f3') f3() f2() f1 () f3() #Error, why? see next section
Or explain it from the perspective that a function is a variable, because f3() is defined inside f1(), which is equivalent to a "local variable", so f3() cannot be accessed outside the function.
3. Example of a decorator without parameters
import time def timer(func): #timer(test1) func=test1 def deco(*args,**kwargs): start_time=time.time() func(*args,**kwargs) #run test1() stop_time = time.time() print("the func run time is %s" %(stop_time-start_time)) return deco @timer # test1 = timer (test1) def test1(): time.sleep(1) print('in the test1')
test1() #Execution
result:
#in the test1
#the func run time is 1.0033304691314697
The code above explains:
The most essential function of the function timer is to return a deco, which is the address space of a function
The next thing to understand is that @timer is equivalent to: test1=timer(test1), timer(test1) will return deco after execution, then executing test1 is equivalent to executing deco, that is, test1()=deco()
So the result of test1() is:
in the test1
the func run time is 1.0033304691314697
Another example:
Author:Yueru Sun import time def timmer(func): def wrapper(*args,**kwargs): start_time=time.time() func(*args,**kwargs) end_time=time.time() print('The running time of func is: %s'%(end_time-start_time)) return wrapper @timmer def test(): time.sleep(2) print('test') test() @timmer def test1(number): time.sleep(1) print('The %s of func is executed'%(number)) test1(1)
Or the above understanding process, sort out the implementation process:
First, the timer function is defined, and then executing @timer is equivalent to executing test1=timer(test1), and returning deco after execution, then test1(number)=deco(number)
Fourth, the decorator example with parameters
If the decorator needs to pass in parameters, then nest the function again on the basis of the above
def timmer1(text): def out_wrapper(func): def wrapper(*args,**kwargs): start_time=time.time() func(*args,**kwargs) end_time=time.time() print('%s func running time is %s'%(text,end_time-start_time)) return wrapper return out_wrapper @timmer1('sun') def test2(): print('test2') test2 ()
The specific understanding will not be repeated, you can review this article:
http://www.cnblogs.com/alex3714/articles/5765046.html
5. Homework
Write a decorator to add authentication functions to multiple functions (the user's account password comes from a file), requiring a successful login, and subsequent functions do not need to enter the user name and password