目錄
- 一、Python 中的作用域規則和嵌套函數
- 二、定義閉包函數
- 三、何時使用閉包?
- 四、總結
一、Python 中的作用域規則和嵌套函數
每當執行一個函數時,就會創建一個新的局部命名空間,它表示包含函數體內分配的函數參數和變量名的局部環境。我們可以將名稱空間看作一個字典,其中鍵是對象名稱,值是對象本身。
解析名稱時,解釋器首先搜索本地命名空間。如果不存在匹配,則搜索全局名稱空間,該名稱空間是定義函數的模塊。如果仍然沒有找到匹配項,則在引發 NameError 異常之前最終檢查內置名稱空間。下圖說明了這一點:

讓我們考慮下面的例子:
age = 27
def birthday():
age = 28
birthday()
print(age) # age will still be 27
>>
27
當變量在函數內部賦值時,它們總是綁定到函數的本地名稱空間; 因此,函數體中的變量 age 指的是一個包含值28的全新對象,而不是外部變量。可以使用全局語句更改此行為。下面的示例強調了這一點:
age = 27
name = "Sarah"
def birthday():
global age # 'age' is in global namespace
age = 28
name = "Roark"
birthday() # age is now 28. name will still be "Sarah"
Python 也支持嵌套函數定義(函數內部的函數):
def countdown(start):
# This is the outer enclosing function
def display():
# This is the nested function
n = start
while n > 0:
n-=1
print('T-minus %d' % n)
display()
# We execute the function
countdown(3)
>>>
T-minus 3
T-minus 2
T-minus 1
二、定義閉包函數
在上面的示例中,如果函數 countdown()的最后一行返回了 display 函數而不是調用它,會發生什么情況?這意味著該函數的定義如下:
def countdown(start):
# This is the outer enclosing function
def display():
# This is the nested function
n = start
while n > 0:
n-=1
print('T-minus %d' % n)
return display
# Now let's try calling this function.
counter1 = countdown(2)
counter1()
>>>
T-minus 2
T-minus 1
使用值2調用 countdown()函數,并將返回的函數綁定到名稱 counter1。在執行 counter1()時,它使用最初提供給 countdown ()的 start 值。因此,在調用 counter1()時,盡管我們已經執行了 count1()函數,但仍然記住這個值。
這種將一些數據(本例中為2)附加到代碼的技術在 Python 中稱為閉包。
即使變量超出范圍或函數本身從當前名稱空間中移除,也會記住封閉范圍中的這個值。我們可以嘗試下面的代碼來確認:
>>> del countdown
>>> counter1()
T-minus 2
T-minus 1
>>> countdown(2)
Traceback (most recent call last):
...
NameError: name 'countdown' is not defined
三、何時使用閉包?
當一個類中實現的方法很少(大多數情況下只有一個方法)時,閉包可以提供一個替代的、更優雅的解決方案。此外,如果我們希望根據延遲或延遲計算的概念編寫代碼,閉包和嵌套函數特別有用。下面是一個例子:
from urllib.request import urlopen
def page(url):
def get():
return urlopen(url).read()
return get
在上面的示例中,page ()函數實際上并不執行任何計算。相反,它只是創建并返回一個函數 get () ,該函數在調用 web 頁面時獲取頁面內容。因此,在 get ()中執行的計算實際上被延遲到計算 get ()時程序中的某個后續點。例如:
>>> url1 = page("http://www.google.com")
>>> url2 = page("http://www.bing.com")
>>> url1
function page.locals>.get at 0x10a6054d0>
>>> url2
function page.locals>.get at 0x10a6055f0>
>>> gdata = url1() # Fetches http://www.google.com
>>> bdata = url2() # Fetches http://www.bing.com
>>>
可以找到閉包函數中包含的值。
所有函數對象都有一個 _closure_ 屬性,如果它是一個閉包函數,那么這個屬性將返回一組單元格對象。根據上面的例子,我們知道 url1和 url2是閉包函數。
>>> page.__closure__ # Returns None since not a closure
>>> url1.__closure__
(cell at 0x10a5f1250: str object at 0x10a5f3120>,)
單元格對象具有存儲關閉值的屬性 cell_contents。
>>> url1.__closure__[0].cell_contents
'http://www.google.com'
>>> url2.__closure__[0].cell_contents
'http://www.bing.com'
四、總結
當嵌套函數引用其封閉范圍中的值時,可以定義 Python 中的閉包。閉包提供了某種形式的數據隱藏。閉包還可以是一種高效的方法,可以在一系列函數調用之間保持狀態。用 Python 創建一個閉包函數:
- 我們必須有一個嵌套的函數
- 嵌套函數必須引用封閉函數中定義的值
- 封閉函數必須返回嵌套函數
到此這篇關于如何理解及使用Python閉包的文章就介紹到這了,更多相關Python閉包內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python高級語法之閉包和裝飾器詳解
- python閉包與引用以及需要注意的陷阱
- 詳解python中的閉包
- Python閉包及裝飾器運行原理解析
- Python閉包與裝飾器原理及實例解析
- Python高級特性之閉包與裝飾器實例詳解
- Python closure閉包解釋及其注意點詳解
- 詳解python中的生成器、迭代器、閉包、裝飾器
- Python閉包和裝飾器用法實例詳解
- Python函數基礎實例詳解【函數嵌套,命名空間,函數對象,閉包函數等】