python中的闭包与装饰器教程
生活随笔
收集整理的這篇文章主要介紹了
python中的闭包与装饰器教程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、閉包
閉包相當于函數中,嵌套另一個函數,并返回。
代碼如下:
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 funcdef test():print('test_func')test = deco(test) # 對函數進行裝飾。執行了deco函數,并將返回值賦值給test >> # 輸出deco的運行結果 decorationtest() # 運行裝飾后的函數 >> test_func2、以上代碼等價于
''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' def deco(func): # 傳入func函數。print('decoration')return func@deco # 等價于上一代碼中test = deco(test),不過上一代碼需放在定義test之后 def test():print('test_func')>> # 輸出deco的運行結果 decorationtest() # 運行裝飾后的函數 >> test_func3、裝飾器(簡版)
def deco(func): # 裝飾函數傳入funcprint('decoration')return func@deco # 裝飾函數。 def test():print('test_func') # 定義完函數后,會直接執行裝飾器deco(test) >> decoration# 調用test,執行test函數 test() >> test_func4、裝飾器(升級版)
在上一個版本中,由于在定義裝飾器 + 函數時,就會執行裝飾函數里面的語句。
為了使其在未被調用時候不執行,需要再嵌套一個函數,將函數進行包裹。
''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' def deco(func): print('decoration') # 此處未調用func函數時,會直接執行def wrapper(): # 名稱自定義,一般用wrapperprint('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 done5 # 此處是test函數的返回值5、裝飾器(進階版)
在包裹函數中,參數形式設置為*arg、**kwarg,會使得函數更加靈活。
當修改test函數參數形式時,不用在裝飾器中同時修改。
import timedef 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 areturn inner@deco def test(a):print('test function:', a)return a# 調用函數 test(5) >> test function: 5 運行時間: 2.00032520294189455 # 5是函數返回的值6、高階版
有時候我們會發現有的裝飾器帶括號,其原因是將上述的裝飾器外面又套了一個函數
''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' import timedef 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 areturn innerreturn deco # 注意:此處需返回裝飾函數@outer() # 此處就需要加括號,其實是調用了outer()函數,將test傳進其子函數 def test(a):print('test function:', a)return atest(4) >> test function: 4 運行時間: 2.0005660057067874 # 返回47、高階終結版
帶參數的裝飾器(裝飾器加括號,帶參數)
import timedef outer(choose): # 在最外層函數中加入參數if choose==1: # 通過choose參數,選擇裝飾器def deco(func):def inner(*arg, **kwarg):print('decoration1')begin_time = time.time()time.sleep(2) # 睡眠2sa = func(*arg, **kwarg) end_time = time.time()print('運行時間1:', end_time - begin_time)return areturn innerreturn decoelse:def deco(func):def inner(*arg, **kwarg): print('decoration2')begin_time = time.time()time.sleep(5) # 睡眠5sa = func(*arg, **kwarg) end_time = time.time()print('運行時間2:', end_time - begin_time)return areturn innerreturn 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秒2 # test1的返回值test2(4) # 調用test2 >> decoration2 test function2: 4 運行時間2: 5.000797986984253 # 5秒4 # test2的返回值結尾給大家推薦一個非常好的學習教程,希望對你學習Python有幫助!
Python基礎入門教程推薦
Python爬蟲案例教程推薦
總結
以上是生活随笔為你收集整理的python中的闭包与装饰器教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中深浅复制教程
- 下一篇: python中的几种倒序遍历