Python裝飾器用法與知識點(diǎn)小結(jié)
本文實(shí)例講述了Python裝飾器用法與知識點(diǎn)。分享給大家供大家參考,具體如下:
(1)裝飾器含參數(shù),被裝飾函數(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)裝飾器實(shí)質(zhì)為函數(shù)內(nèi)嵌,返回函數(shù)地址。
(2)裝飾器帶參數(shù)與不帶參數(shù)相比裝飾器帶參數(shù)的多了一層函數(shù)定義用于接收裝飾器中傳遞的參數(shù),其余基本相同。
(3)先驗(yàn)證裝飾器中的參數(shù),在驗(yàn)證普通函數(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)一邊計(jì)算的機(jī)制,稱為生成器:generator。
生成器只有在調(diào)用時(shí)才會生成相應(yīng)的數(shù)據(jù),只記錄當(dāng)前位置。
只有一個(gè)__next__()方法
第二種方式通過yield生成
在函數(shù)中使用yield即可將一個(gè)函數(shù)變?yōu)橐粋€(gè)生成器
迭代器:
直接作用于for循環(huán)的數(shù)據(jù)類型:
一類是集合數(shù)據(jù)類型,如list、tuple、dict、set、str等;
一類是generator,包括生成器和帶yield的generator function。
直接作用于for循環(huán)的對象統(tǒng)稱為可迭代對象:Iterable。
可以使用isinstance()判斷一個(gè)對象是否是Iterable對象
from collections import Iterable isinstance([], Iterable)=========true
*可以被next()函數(shù)調(diào)用并不斷返回下一個(gè)值的對象稱為迭代器:Iterator。
可以使用isinstance()判斷一個(gè)對象是否是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對象表示的是一個(gè)數(shù)據(jù)流,Iterator對象可以被next()函數(shù)調(diào)用并不斷返回下一個(gè)數(shù)據(jù),直到?jīng)]有數(shù)據(jù)時(shí)拋出StopIteration錯(cuò)誤。可以把這個(gè)數(shù)據(jù)流看做是一個(gè)有序序列,但我們卻不能提前知道序列的長度,只能不斷通過next()函數(shù)實(shí)現(xiàn)按需計(jì)算下一個(gè)數(shù)據(jù),所以Iterator的計(jì)算是惰性的,只有在需要返回下一個(gè)數(shù)據(jù)時(shí)它才會計(jì)算。
Iterator甚至可以表示一個(gè)無限大的數(shù)據(jù)流,例如全體自然數(shù)。而使用list是永遠(yuǎn)不可能存儲全體自然數(shù)的。
小結(jié):
凡是可作用于for循環(huán)的對象都是Iterable類型;
凡是可作用于next()函數(shù)的對象都是Iterator類型,它們表示一個(gè)惰性計(jì)算的序列;
集合數(shù)據(jù)類型如list、dict、str等是Iterable但不是Iterator,不過可以通過iter()函數(shù)獲得一個(gè)Iterator對象。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. PHP使用Swagger生成好看的API文檔2. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條3. Python3 json模塊之編碼解碼方法講解4. Python 制作查詢商品歷史價(jià)格的小工具5. Python 如何調(diào)試程序崩潰錯(cuò)誤6. Python 利用Entrez庫篩選下載PubMed文獻(xiàn)摘要的示例7. ASP基礎(chǔ)知識VBScript基本元素講解8. python使用jenkins發(fā)送企業(yè)微信通知的實(shí)現(xiàn)9. Python sublime安裝及配置過程詳解10. Python 合并拼接字符串的方法

網(wǎng)公網(wǎng)安備