1. Call Order of functions
Wrong way to call
1 #! /user/bin/env Ptyhon2 #-*-coding:utf-8-*-3 #Author:visonwong4 5 #How to call a function error6 defFunc ():#define function func ()7 Print("In the func")8Foo ()#Call function foo ()9 TenFunc ()#Execute function func () One A defFoo ():#define function foo () - Print("In the foo")
Execution Result:
1E:\Python\PythonLearing\venv\Scripts\python.exe e:/python/pythonlearing/func.py2 inchThe func3 Traceback (most recent):4File"e:/python/pythonlearing/func.py", line 11,inch<module>5Func ()#Execute function func ()6File"e:/python/pythonlearing/func.py", line 8,inchfunc7Foo ()#Call function foo ()8Nameerror:name'Foo' is notdefined9 TenProcess finished with exit code 1
The correct way to call
1 #! /user/bin/env Ptyhon2 #-*-coding:utf-8-*-3 #Author:visonwong4 5 #How the function is called correctly6 defFunc ():#define function func ()7 Print("In the func")8Foo ()#Call function foo ()9 defFoo ():#define function foo ()Ten Print("In the foo") OneFunc ()#Execute function func ()
Execution Result:
1 E:\Python\PythonLearing\venv\Scripts\python.exe e:/python/pythonlearing/func.py2 inch The func 3 inch The foo 4 5 Process finished with exit code 0
Summary: The called function is defined before execution.
2. High-order function
A function is a higher order function if one of the following conditions is met
The function in the call order is slightly modified as a higher-order function.
1 #! /user/bin/env Ptyhon2 #-*-coding:utf-8-*-3 #Author:visonwong4 5 #Higher order functions6 defFunc ():#define function func ()7 Print("In the func")8 returnFoo ()#Call function foo ()9 Ten defFoo ():#define function foo () One Print("In the foo") A return100 - -res = func ()#Execute function func () the Print(RES)#Print function return value
Output Result:
1 E:\Python\PythonLearing\venv\Scripts\python.exe e:/python/pythonlearing/func.py2 inch The func 3 inch The foo 4 56 Process finished with exit code 0
From the above program learned that the return value of function Func is the return value of function foo, if Foo does not define the return value, the return value of func defaults to none;
Here's a look at more complex higher-order functions:
1 #! /user/bin/env Ptyhon2 #-*-coding:utf-8-*-3 #Author:visonwong4 5 #more complex higher-order functions6 ImportTime#Call Module time7 8 defBar ():9Time.sleep (1)Ten Print("In the bar") One A deffoo (func): -Start_time =time.time () - func () theEnd_time =time.time () - Print("func runing time is%s"% (End_time-start_time)) - -Foo (BAR)
Execution Result:
1 E:\Python\PythonLearing\venv\Scripts\python.exe e:/python/pythonlearing/func.py2 inch The bar 3 is 1.040059328079223645 Process finished with exit code 0
In fact, the above code has implemented the adorner function, that is, the bar () does not modify the case, the bar () added functionality, but changed the bar () call mode
Below we modify the code above to add the function without changing the bar () call mode
9--function Anatomy of Python learning Path