13. 閉包
(1) global, nonlocal及local變數
以下是一個例子:
cnt1 = 0
def outer_func(name):
cnt2 = 0
def inner_func():
global cnt1
nonlocal cnt2
cnt1+=1
cnt2+=1
cnt3 = cnt1 - cnt2
print(f'函式:{name}, 所有函式呼叫次數:{cnt1}, 自己呼叫次數:{cnt2}, 其他函式呼叫次數:{cnt3}')
return inner_func主程式:
執行結果:
說明

(2) 閉包, closure
以下是一個例子:
主程式:
執行結果:
說明:

問題:
Last updated