def test1(): print("--- in test1 func----") # 調用函數 test1() # 引用函數 ret = test1 print(id(ret)) print(id(test1)) #經過引用調用函數 ret()
運行結果:python
--- in test1 func---- 140212571149040 140212571149040 --- in test1 func----
# 定義一個函數 def test(number): # 在函數內部再定義一個函數,而且這個函數用到了外邊函數的變量,那麼將這個函數以及用到的一些變量稱之爲閉包 def test_in(number_in): print("in test_in 函數, number_in is %d" % number_in) return number+number_in # 其實這裏返回的就是閉包的結果 return test_in # 給test函數賦值,這個20就是給參數number ret = test(20) # 注意這裏的100其實給參數number_in print(ret(100)) #注 意這裏的200其實給參數number_in print(ret(200))
運行結果:閉包
in test_in 函數, number_in is 100 120 in test_in 函數, number_in is 200 220
def line_conf(a, b): def line(x): return a*x + b return line line1 = line_conf(1, 1) line2 = line_conf(4, 5) print(line1(5)) print(line2(5))
這個例子中,函數line與變量a,b構成閉包。在建立閉包的時候,咱們經過line_conf的參數a,b說明了這兩個變量的取值,這樣,咱們就肯定了函數的最終形式(y = x + 1和y = 4x + 5)。咱們只須要變換參數a,b,就能夠得到不一樣的直線表達函數。由此,咱們能夠看到,閉包也具備提升代碼可複用性的做用。函數
若是沒有閉包,咱們須要每次建立直線函數的時候同時說明a,b,x。這樣,咱們就須要更多的參數傳遞,也減小了代碼的可移植性。spa
注意點:code
因爲閉包引用了外部函數的局部變量,則外部函數的局部變量沒有及時釋放,消耗內存內存
def counter(start=0): def incr(): nonlocal start start += 1 return start return incr c1 = counter(5) print(c1()) print(c1()) c2 = counter(50) print(c2()) print(c2()) print(c1()) print(c1()) print(c2()) print(c2())
def counter(start=0): count=[start] def incr(): count[0] += 1 return count[0] return incr c1 = closeure.counter(5) print(c1()) # 6 print(c1()) # 7 c2 = closeure.counter(100) print(c2()) # 101 print(c2()) # 102