Python閉包與裝飾器原理及例項解析
阿新 • • 發佈:2020-05-02
一、閉包
閉包相當於函式中,巢狀另一個函式,並返回。程式碼如下:
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的返回值
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。