Python learning road 9 - function analysis

  1. Function calling sequence

   wrong call

 

1  # ! /user/bin/env ptyhon 
2  # -*- coding:utf-8 -*- 
3  # Author: VisonWong 
4  
5  #The wrong way to call the function 
6  def func():   #Define the function func() 
7      print ( " in the func " )
 8      foo()   #call function foo() 
9  
10 func()   #execute function func() 
11  
12  def foo():   #define function foo() 
13      print ( " in the foo " )

 

    Results of the:

 1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/func.py
 2 in the func
 3 Traceback (most recent call last):
 4   File "E:/Python/PythonLearing/func.py", line 11, in <module>
 5     func()  # 执行函数func()
 6   File "E:/Python/PythonLearing/func.py", line 8, in func
 7     foo()  # 调用函数foo()
 8 NameError: name 'foo' is not defined
 9 
10 Process finished with exit code 1

    correct way to call

1  # ! /user/bin/env ptyhon 
2  # -*- coding:utf-8 -*- 
3  # Author: VisonWong 
4  
5  #The correct way to call the function 
6  def func():                      #Define the function func() 
7      print ( " in the func " )
 8      foo()                        #call function foo() 
9  def foo():                       #define function foo() 
10      print ( " in the foo " )
 11 func()                          #Execute function func()

   Results of the:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/func.py
2 in the func
3 in the foo
4 
5 Process finished with exit code 0

      Summary: The called function must be defined before execution.

   2. Higher order functions 

A function can be a higher-order function if one of the following conditions is met

 

    • A function is passed as a parameter to another function

    • The return value of a function contains one or more functions

 

The function in the calling sequence just now is a higher-order function with a slight modification

1  # ! /user/bin/env ptyhon 
2  # -*- coding:utf-8 -*- 
3  # Author: VisonWong 
4  
5  #Higher order function 
6  def func():   #Define function func() 
7      print ( " in the func " )
 8      return foo()   #call function foo() 
9  
10  def foo():   #define function foo() 
11      print ( " in the foo " )
 12      return 100
 13  
14res = func() #Execute   function func() 
15  print (res) #Print   function return value

  Output result:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/func.py
2 in the func
3 in the foo
4 100
5 
6 Process finished with exit code 0

  From the above program, we know that the return value of the function func is the return value of the function foo. If foo does not define a return value, the return value of func defaults to None;

  Let's look at a more complex higher-order function:

1  # ! /user/bin/env ptyhon 
2  # -*- coding:utf-8 -*- 
3  # Author: VisonWong 
4  
5  #More complex higher-order functions 
6  import time   #calling module time 
7  
8  def bar():
 9      time .sleep(1 )
 10      print ( " in the bar " )
 11  
12  def foo(func):
 13      start_time = time.time()
 14      func()
 15      end_time = time.time()
 16     print("func runing time is %s" % (end_time - start_time))
17 
18 foo(bar)

 

 

  Results of the:  

 

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/func.py
2 in the bar
3 func runing time is 1.0400593280792236
4 
5 Process finished with exit code 0

 

  In fact, the above code has implemented some functions of the decorator, that is, adding functions to bar() without modifying the bar() code; but changing the calling method of bar()

  Next, we will modify the above code and add functions without changing the calling method of bar().

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325386543&siteId=291194637
Recommended