Python 閉包的使用方法
嵌套函數中的非局部變量
在進入閉包之前,我們必須先了解一個嵌套函數和非局部變量。
在函數中定義另一個函數稱為嵌套函數。嵌套函數可以訪問包圍范圍內的變量。
在Python中,這些非局部變量只能在默認情況下讀取,我們必須將它們顯式地聲明為非局部變量(使用nonlocal關鍵字)才能進行修改。
以下是訪問非局部變量的嵌套函數的示例。
def print_msg(msg):# This is the outer enclosing function def printer():# This is the nested function print(msg) printer()# We execute the function# Output: Helloprint_msg("Hello")可以看到嵌套函數printer()能夠訪問封閉函數的非局部變量msg。
定義閉包函數
在上面的例子中,如果函數print_msg()的最后一行返回printer()函數而不是調用它,會發生什么? 如該函數定義如下 -
def print_msg(msg):# This is the outer enclosing function def printer():# This is the nested function print(msg) return printer # this got changed# Now let's try calling this function.# Output: Helloanother = print_msg("Hello")another()這樣是不尋常的。
print_msg()函數使用字符串“Hello”進行調用,返回的函數被綁定到另一個名稱。 在調用another()時,盡管我們已經完成了print_msg()函數的執行,但仍然記住了這個消息。
一些數據(“Hello”)附加到代碼中的這種技術在Python中稱為閉包。
即使變量超出范圍或函數本身從當前命名空間中刪除,也會記住封閉范圍內的值。
嘗試在Python shell中運行以下內容以查看輸出。
>>> del print_msg>>> another()Hello>>> print_msg("Hello")Traceback (most recent call last):...NameError: name 'print_msg' is not defined什么時候閉包?
從上面的例子可以看出,當嵌套函數引用其封閉范圍內的值時,在Python中有使用了一個閉包。
在Python中創建閉包必須滿足的標準將在以下幾點 -
必須有一個嵌套函數(函數內部的函數)。 嵌套函數必須引用封閉函數中定義的值。 閉包函數必須返回嵌套函數。何時使用閉包?
那么閉包是什么好的?
閉包可以避免使用全局值并提供某種形式的數據隱藏。它還可以提供面向對象的解決問題的解決方案。
當在類中幾乎沒有方法(大多數情況下是一種方法)時,閉包可以提供一個替代的和更優雅的解決方案。 但是當屬性和方法的數量變大時,更好地實現一個類。
這是一個簡單的例子,其中閉包可能比定義類和創建對象更為優先。
def make_multiplier_of(n): def multiplier(x): return x * n return multiplier# Multiplier of 3times3 = make_multiplier_of(3)# Multiplier of 5times5 = make_multiplier_of(5)# Output: 27print(times3(9))# Output: 15print(times5(3))# Output: 30print(times5(times3(2)))
新聞熱點
疑難解答