9--function Anatomy of Python learning Path

Source: Internet
Author: User

   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

      • A function is passed into another function as a parameter

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

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.