Order of function calls
Similar to other languages. python functions are not allowed to be referenced and invoked until they are declared.
1. Calls or references will report errors before they are declared. 2 def t1(): 3 print('This is t1...') 4 t2() 5 6 t1() 7 8 def t2(): 9 print('This is t2...') 10 11 12 Display results: 13 This is t1... 14 Traceback (most recent call last): 15 File "D:/Software/pychar/data/s13/practise1.py", line 37, in <module> 16 t1() 17 File "D:/Software/pychar/data/s13/practise1.py", line 35, in t1 18 t2() 19 NameError: name 't2' is not defined 20 21 22 23.2. Correct order of call: (declare function first, refer to or call) 24 def t1(): 25 print('This is t1...') 26 t2() 27 28 def t2(): 29 print('This is t2...') 30 31 t1() 32 33 display results: 34 This is t1... 35 This is t2...
Functions and Functions of Functions
You need to add a print log function.
When no function is learned, print can only be displayed, for example:
1 def t1(): 2 pass 3 print('logging') 4 5 def t2(): 6 pass 7 print('logging')
After learning a function, you can define a function, for example:
def logger(): print('logging') def t1(): pass logger() #Direct call function def t2(): pass logger()
Functions and Characteristics of Decorators
Additional functionality is required for code that is already online:
Question:
1. There are many lines of code, so it's very troublesome to find them one by one, and add new functions to them.
2. The code has been put on line. Modification of the source code may lead to unknown failures. (Additional functionality is not allowed to modify the source code)
3. The code has been put on line, and the code without light source can not be modified freely, even the way of function call can not be modified freely.
Solution: Use decorator to decorate code.
Overview of Decorators
Definition:
Decorator itself is a function, following the declaration, reference, invocation, scope and other relevant rules of function.
II. Role:
Decorator: The purpose is to add additional functions to other functions.
Purpose:
Decorator: The most common uses are user login and authority authentication.
IV. Principles:
Knowledge Reserve of Decorators
Knowledge Point 1: Function is "Variable":
Defining an x or Y is to declare a memory block in memory and assign it to x or y by memory address. When invoking x or y, it can be loaded into the memory block for reference or invocation.
The principle of defining a function of test is the same as above.
The same is true for defining an anonymous body of functions.
Knowledge Point 2. Higher Order Functions:
Practice a:
A function is passed into another function as a parameter.1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 # Author:Dong Ye 4 5 import time 6 7 #Define a source code. Decoration is required without changing source code and invocation mode: 8 def bar(): #Define a variable to simulate the function body on the line 9 time.sleep(3) 10 print("in the bar") 11 12 13 #Decorate with higher-order functions bar Code 14 def test1(func): #Define a parameter for receiving calls test1 The argument of a function, if the name of the function is passed in, then the function has been applied. 15 start_time = time.time() 16 func() #call bar Function, similar to x=1,y=x display y. and func = bar,Direct call func 17 stop_time = time.time() 18 print("the func run time is %s (non bar run time)" %(stop_time-start_time)) 19 20 test1(bar) #Call the mock decorator to pass arguments to test1(Functions that simulate online environments)