嵌套函數與非局部變量
在函數中定義另一個函數稱為嵌套函數。嵌套函數可以訪問包圍范圍內的變量。
def print_msg(msg): def printer(): print(msg) printer() print_msg("Hello") #Hello
nonlocal關鍵字
使用nonlocal關鍵字可以修改外層函數中變量的值:
def outer(): a = 1
def inner(): a += 1
print("Inner", a) inner() print("Outer", a) outer() #UnboundLocalError: local variable 'a' referenced before assignment
def outer(): a = 1
def inner(): nonlocal a a += 1
print("Inner", a) inner() print("Outer", a) outer()
輸出:
Inner 2 Outer 2
閉包
閉包也稱詞法閉包,如果在一個內部函數里,對在外部作用域(但不是在全局作用域)的變量進行引用,那么內部函數就被認為是閉包(closure),這里說的作用域就是nonlocal;通俗來講,閉包就是把一個函數(方法)作為一個變量來使用。
在Python中創建閉包必須滿足的標准將在以下幾點:
- 必須有一個嵌套函數(函數內部的函數)。
- 嵌套函數必須引用封閉函數中定義的值。
- 閉包函數必須返回嵌套函數。
利用閉包實現一個計數器:
def counter(): i = 0 def nested(): nonlocal i i += 1
return i return nested c = counter() print(c(),c(),c(),end=" ") #1 2 3
返回的函數並沒有立刻執行,而是直到調用了c()才執行。
返回的函數中不要引用循環變量:
def count(): fs = [] for i in range(1, 4): def f(): return i*i fs.append(f) return fs f1, f2, f3 = count() print(f1()) #9
print(f2()) #9
print(f3()) #9
全部都是9!原因就在於返回的函數引用了變量i,但它並非立刻執行。等到3個函數都返回時,它們所引用的變量i已經變成了3,因此最終結果為9。
如果一定要使用循環變量,解決方法是再創建一個函數,用該函數的參數綁定循環變量當前的值,無論該循環變量后續如何更改,已綁定到函數參數的值不變:
def count(): def f(j): def g(): return j*j return g fs = [] for i in range(1, 4): fs.append(f(i)) # f(i)立刻被執行,因此i的當前值被傳入f()
return fs f1, f2, f3 = count() print(f1()) #1
print(f2()) #4
print(f3()) #9