溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何正確的使用Python閉包

發(fā)布時(shí)間:2021-06-01 16:22:50 來源:億速云 閱讀:129 作者:Leah 欄目:開發(fā)技術(shù)

如何正確的使用Python閉包?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

一、Python 中的作用域規(guī)則和嵌套函數(shù)

每當(dāng)執(zhí)行一個(gè)函數(shù)時(shí),就會(huì)創(chuàng)建一個(gè)新的局部命名空間,它表示包含函數(shù)體內(nèi)分配的函數(shù)參數(shù)和變量名的局部環(huán)境。我們可以將名稱空間看作一個(gè)字典,其中鍵是對象名稱,值是對象本身。

解析名稱時(shí),解釋器首先搜索本地命名空間。如果不存在匹配,則搜索全局名稱空間,該名稱空間是定義函數(shù)的模塊。如果仍然沒有找到匹配項(xiàng),則在引發(fā) NameError 異常之前最終檢查內(nèi)置名稱空間。下圖說明了這一點(diǎn):

如何正確的使用Python閉包

讓我們考慮下面的例子:

age = 27
def birthday(): 
  age = 28
birthday()
print(age)  # age will still be 27
>>
27

當(dāng)變量在函數(shù)內(nèi)部賦值時(shí),它們總是綁定到函數(shù)的本地名稱空間; 因此,函數(shù)體中的變量 age 指的是一個(gè)包含值28的全新對象,而不是外部變量??梢允褂萌终Z句更改此行為。下面的示例強(qiáng)調(diào)了這一點(diǎn):

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 也支持嵌套函數(shù)定義(函數(shù)內(nèi)部的函數(shù)):

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

二、定義閉包函數(shù)

在上面的示例中,如果函數(shù) countdown()的最后一行返回了 display 函數(shù)而不是調(diào)用它,會(huì)發(fā)生什么情況?這意味著該函數(shù)的定義如下:

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調(diào)用 countdown()函數(shù),并將返回的函數(shù)綁定到名稱 counter1。在執(zhí)行 counter1()時(shí),它使用最初提供給 countdown ()的 start 值。因此,在調(diào)用 counter1()時(shí),盡管我們已經(jīng)執(zhí)行了 count1()函數(shù),但仍然記住這個(gè)值。

這種將一些數(shù)據(jù)(本例中為2)附加到代碼的技術(shù)在 Python 中稱為閉包。

即使變量超出范圍或函數(shù)本身從當(dāng)前名稱空間中移除,也會(huì)記住封閉范圍中的這個(gè)值。我們可以嘗試下面的代碼來確認(rèn):

>>> del countdown
>>> counter1()
T-minus 2
T-minus 1
>>> countdown(2)
Traceback (most recent call last):
...
NameError: name 'countdown' is not defined

三、何時(shí)使用閉包?

當(dāng)一個(gè)類中實(shí)現(xiàn)的方法很少(大多數(shù)情況下只有一個(gè)方法)時(shí),閉包可以提供一個(gè)替代的、更優(yōu)雅的解決方案。此外,如果我們希望根據(jù)延遲或延遲計(jì)算的概念編寫代碼,閉包和嵌套函數(shù)特別有用。下面是一個(gè)例子:

from urllib.request import urlopen
def page(url): 
  def get(): 
    return urlopen(url).read() 
  return get

在上面的示例中,page ()函數(shù)實(shí)際上并不執(zhí)行任何計(jì)算。相反,它只是創(chuàng)建并返回一個(gè)函數(shù) get () ,該函數(shù)在調(diào)用 web 頁面時(shí)獲取頁面內(nèi)容。因此,在 get ()中執(zhí)行的計(jì)算實(shí)際上被延遲到計(jì)算 get ()時(shí)程序中的某個(gè)后續(xù)點(diǎn)。例如:

>>> 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
>>>

可以找到閉包函數(shù)中包含的值。

所有函數(shù)對象都有一個(gè) _closure_ 屬性,如果它是一個(gè)閉包函數(shù),那么這個(gè)屬性將返回一組單元格對象。根據(jù)上面的例子,我們知道 url1和 url2是閉包函數(shù)。

>>> page.__closure__       # Returns None since not a closure
>>> url1.__closure__
(<cell at 0x10a5f1250: str object at 0x10a5f3120>,)

單元格對象具有存儲(chǔ)關(guān)閉值的屬性 cell_contents。

>>> url1.__closure__[0].cell_contents
'http://www.google.com'
>>> url2.__closure__[0].cell_contents
'http://www.bing.com'

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI