Python裝飾器用法與知識點小結(jié)
本文實例講述了Python裝飾器用法與知識點。分享給大家供大家參考,具體如下:
(1)裝飾器含參數(shù),被裝飾函數(shù)不含(含)參數(shù)
實例代碼如下:
import time# 裝飾器函數(shù)def wrapper(func): def done(*args,**kwargs): start_time = time.time() func(*args,**kwargs) stop_time = time.time() print(’the func run time is %s’ % (stop_time - start_time)) return done# 被裝飾函數(shù)1@wrapperdef test1(): time.sleep(1) print('in the test1')# 被裝飾函數(shù)2@wrapperdef test2(name): #1.test2===>wrapper(test2) 2.test2(name)==dome(name) time.sleep(2) print('in the test2,the arg is %s'%name)# 調(diào)用test1()test2('Hello World')
(2)裝飾器含有參數(shù),被裝飾函數(shù)含(不含)參數(shù)
import timeuser,passwd = ’admin’,’admin’def auth(auth_type): print('auth func:',auth_type) def outer_wrapper(func): def wrapper(*args, **kwargs): print('wrapper func args:', *args, **kwargs) if auth_type == 'local':username = input('Username:').strip()password = input('Password:').strip()if user == username and passwd == password: print('033[32;1mUser has passed authentication033[0m') res = func(*args, **kwargs) # from home print('---after authenticaion ') return reselse: exit('033[31;1mInvalid username or password033[0m') elif auth_type == 'ldap':print('ldap鏈接') return wrapper return outer_wrapper@auth(auth_type='local') # home = wrapper()def home(): print('welcome to home page') return 'from home'@auth(auth_type='ldap')def bbs(): print('welcome to bbs page'print(home()) #wrapper()bbs()
總結(jié):
(1)裝飾器實質(zhì)為函數(shù)內(nèi)嵌,返回函數(shù)地址。
(2)裝飾器帶參數(shù)與不帶參數(shù)相比裝飾器帶參數(shù)的多了一層函數(shù)定義用于接收裝飾器中傳遞的參數(shù),其余基本相同。
(3)先驗證裝飾器中的參數(shù),在驗證普通函數(shù)的參數(shù)
小知識:
列表生產(chǎn)式:[i for i in range(5)]---->[0,1,2,3,4,5]
生成器與迭代器:
第一種方式通過括號的方式生成
生成器:()---(i for i in range(5)) ==>generator
這種一邊循環(huán)一邊計算的機制,稱為生成器:generator。
生成器只有在調(diào)用時才會生成相應(yīng)的數(shù)據(jù),只記錄當前位置。
只有一個__next__()方法
第二種方式通過yield生成
在函數(shù)中使用yield即可將一個函數(shù)變?yōu)橐粋€生成器
迭代器:
直接作用于for循環(huán)的數(shù)據(jù)類型:
一類是集合數(shù)據(jù)類型,如list、tuple、dict、set、str等;
一類是generator,包括生成器和帶yield的generator function。
直接作用于for循環(huán)的對象統(tǒng)稱為可迭代對象:Iterable。
可以使用isinstance()判斷一個對象是否是Iterable對象
from collections import Iterable isinstance([], Iterable)=========true
*可以被next()函數(shù)調(diào)用并不斷返回下一個值的對象稱為迭代器:Iterator。
可以使用isinstance()判斷一個對象是否是Iterator對象:
>>> from collections import Iterator>>> isinstance((x for x in range(10)), Iterator)======>True
生成器都是Iterator對象,但list、dict、str雖然是Iterable,卻不是Iterator。
把list、dict、str等Iterable變成Iterator可以使用iter()函數(shù):
例如:iter([])<====迭代器
Python的Iterator對象表示的是一個數(shù)據(jù)流,Iterator對象可以被next()函數(shù)調(diào)用并不斷返回下一個數(shù)據(jù),直到?jīng)]有數(shù)據(jù)時拋出StopIteration錯誤。可以把這個數(shù)據(jù)流看做是一個有序序列,但我們卻不能提前知道序列的長度,只能不斷通過next()函數(shù)實現(xiàn)按需計算下一個數(shù)據(jù),所以Iterator的計算是惰性的,只有在需要返回下一個數(shù)據(jù)時它才會計算。
Iterator甚至可以表示一個無限大的數(shù)據(jù)流,例如全體自然數(shù)。而使用list是永遠不可能存儲全體自然數(shù)的。
小結(jié):
凡是可作用于for循環(huán)的對象都是Iterable類型;
凡是可作用于next()函數(shù)的對象都是Iterator類型,它們表示一個惰性計算的序列;
集合數(shù)據(jù)類型如list、dict、str等是Iterable但不是Iterator,不過可以通過iter()函數(shù)獲得一個Iterator對象。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章:
1. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究2. 三個不常見的 HTML5 實用新特性簡介3. Angular獲取ngIf渲染的Dom元素示例4. IIS+PHP添加對webp格式圖像的支持配置方法5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. 無線標記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁7. 使用.net core 自帶DI框架實現(xiàn)延遲加載功能8. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析9. php測試程序運行速度和頁面執(zhí)行速度的代碼10. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析
