歡迎您光臨本站 註冊首頁

Python閉包與裝飾器原理及實例解析

←手機掃碼閱讀     kyec555 @ 2020-05-01 , reply:0

一、閉包

閉包相當於函數中,嵌套另一個函數,並返回。代碼如下:

def func(name): # 定義外層函數 def inner_func(age): # 內層函數 print('name: ', name, ', age: ', age) return inner_func # 注意此處要返回,才能體現閉包 bb = func('jayson') # 將字符串傳給func函數,返回inner_func並賦值給變量 bb(28) # 通過變量調用func函數,傳入參數,從而完成閉包 >> name: jayson , age: 28

二、裝飾器

裝飾器:把函數test當成變量傳入裝飾函數deco --> 執行了裝飾操作後,變量傳回給了函數test()。比如裝飾器效果是test = test-1,test函數經過deco裝飾後,調用test其實執行的是 test = test-1。

1、裝飾器是利用閉包原理,區別是裝飾器在閉包中傳入的參數是函數,而不是變量。

注:其實在裝飾器中,函數即變量

def deco(func): # 傳入func函數。 print('decoration') return func def test(): print('test_func') test = deco(test) # 對函數進行裝飾。執行了deco函數,並將返回值賦值給test >> # 輸出deco的運行結果 decoration test() # 運行裝飾後的函數 >> test_func

2、以上代碼等價於

def deco(func): # 傳入func函數。 print('decoration') return func @deco # 等價於上一代碼中test = deco(test),不過上一代碼需放在定義test之後 def test(): print('test_func') >> # 輸出deco的運行結果 decoration test() # 運行裝飾後的函數 >> test_func

3、裝飾器(簡版)

def deco(func): # 裝飾函數傳入func print('decoration') return func @deco # 裝飾函數。 def test(): print('test_func') # 定義完函數後,會直接執行裝飾器deco(test) >> decoration # 調用test,執行test函數 test() >> test_func

3、裝飾器(升級版)

在上一個版本中,由於在定義裝飾器 + 函數時,就會執行裝飾函數裡面的語句。

為了使其在未被調用時候不執行,需要再嵌套一個函數,將函數進行包裹。

def deco(func): print('decoration') # 此處未調用func函數時,會直接執行 def wrapper(): # 名稱自定義,一般用wrapper print('execute') # 此處未調用func函數時,不會執行 func() # 執行函數 return wrapper # 此處返回wrapper給func,通過外部func()執行 @deco # 注意:此處不能有括號。有括號的形式是func未傳入最外層deco(),傳入deco的子函數中 def test(): print('test_func') >> decoration #調用test test() >> execute test_func

注意:如果func函數本身有返回值,同樣需要在包裹函數中返回

def deco(func): print('decoration') def wrapper(): print('execute') a = func() # 執行函數,並返回值 print('done') return a # 將func的返回值一併返回 return wrapper @deco def test(): print('test_func') return 5 # 增加返回值 >> decoration #調用test test() >> execute test_func done # 此處是test函數的返回值

3、裝飾器(進階版)

在包裹函數中,參數形式設置為*arg、**kwarg,會使得函數更加靈活。

當修改test函數參數形式時,不用在裝飾器中同時修改。

import time def deco(func): def inner(*arg, **kwarg): # 此處傳入參數 begin_time = time.time() time.sleep(2) a = func(*arg, **kwarg) # 調用函數,使用傳入的參數 end_time = time.time() print('運行時間:', end_time - begin_time) return a return inner @deco def test(a): print('test function:', a) return a # 調用函數 test(5) >> test function: 5 運行時間: 2.0003252029418945 # 5是函數返回的值

4、高階版

有時候我們會發現有的裝飾器帶括號,其原因是將上述的裝飾器外面又套了一個函數

import time def outer(): # 在原裝飾器外套一層函數,將裝飾器封裝在函數裡面。(outer自定義) def deco(func): # 原裝飾器,後面的代碼一樣 def inner(*arg, **kwarg): begin_time = time.time() time.sleep(2) a = func(*arg, **kwarg) end_time = time.time() print('運行時間:', end_time - begin_time) return a return inner return deco # 注意:此處需返回裝飾函數 @outer() # 此處就需要加括號,其實是調用了outer()函數,將test傳進其子函數 def test(a): print('test function:', a) return a test(4) >> test function: 4 運行時間: 2.000566005706787 # 返回4

5、高階終結版

帶參數的裝飾器(裝飾器加括號,帶參數)

import time def outer(choose): # 在最外層函數中加入參數 if choose==1: # 通過choose參數,選擇裝飾器 def deco(func): def inner(*arg, **kwarg): print('decoration1') begin_time = time.time() time.sleep(2) # 睡眠2s a = func(*arg, **kwarg) end_time = time.time() print('運行時間1:', end_time - begin_time) return a return inner return deco else: def deco(func): def inner(*arg, **kwarg): print('decoration2') begin_time = time.time() time.sleep(5) # 睡眠5s a = func(*arg, **kwarg) end_time = time.time() print('運行時間2:', end_time - begin_time) return a return inner return deco @outer(1) # 由於outer中有參數,此處必須傳入參數 def test1(a): print('test function1:', a) return a @outer(5) # 傳入另一個參數 def test2(a): print('test function2:', a) return a # 分別調用2個函數(2個函數裝飾器相同,裝飾器參數不同) test1(2) # 調用test1 >> decoration1 test function1: 2 運行時間1: 2.000072717666626 # 2秒 # test1的返回值 test2(4) # 調用test2 >> decoration2 test function2: 4 運行時間2: 5.000797986984253 # 5秒 # test2的返回值


[kyec555 ] Python閉包與裝飾器原理及實例解析已經有283次圍觀

http://coctec.com/docs/python/shhow-post-232398.html