Python學習之路9——函數剖析

來源:互聯網
上載者:User

標籤:模組   font   lin   line   use   pytho   改變   tar   nis   

  1、函數的調用順序

   錯誤的調用方式

 

 1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4  5 # 函數錯誤的調用方式 6 def func():  # 定義函數func() 7     print("in the func") 8     foo()  # 調用函數foo() 9 10 func()  # 執行函數func()11 12 def foo():  # 定義函數foo()13     print("in the foo")

 

    執行結果:

 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

    正確的調用方式

 1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4  5 #函數正確的調用方式 6 def func():                     #定義函數func() 7     print("in the func") 8     foo()                       #調用函數foo() 9 def foo():                      #定義函數foo()10     print("in the foo")11 func()                          #執行函數func()

   執行結果:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/func.py2 in the func3 in the foo4 5 Process finished with exit code 0

      總結:被調用函數要在執行之前被定義。

   2、高階函數 

滿足下列條件之一就可成函數為高階函數

 

    • 某一函數當做參數傳入另一個函數中

    • 函數的傳回值包含一個或多個函數

 

剛才調用順序中的函數稍作修改就是一個高階函數

 1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4  5 # 高階函數 6 def func():  # 定義函數func() 7     print("in the func") 8     return foo()  # 調用函數foo() 9 10 def foo():  # 定義函數foo()11     print("in the foo")12     return 10013 14 res = func()  # 執行函數func()15 print(res)  # 列印函數傳回值

  輸出結果:

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/func.py2 in the func3 in the foo4 1005 6 Process finished with exit code 0

  從上面的程式得知函數func的傳回值為函數foo的傳回值,如果foo不定義傳回值的話,func的傳回值預設為None;

  下面來看看更複雜的高階函數:

 1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4  5 # 更複雜的高階函數 6 import time  # 調用模組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)

 

 

  執行結果:  

 

1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/func.py2 in the bar3 func runing time is 1.04005932807922364 5 Process finished with exit code 0

 

  其實上面這段代碼已經實現了裝飾器一些功能,即在不修改bar()代碼的情況下,給bar()添加了功能;但是改變了bar()調用方式

  下面我們對上面的code進行下修改,不改變bar()調用方式的情況下進行功能添加

 

Python學習之路9——函數剖析

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.