Python裝飾器用法與知識(shí)點(diǎn)小結(jié)
本文實(shí)例講述了Python裝飾器用法與知識(shí)點(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ù)
小知識(shí):
列表生產(chǎn)式:[i for i in range(5)]---->[0,1,2,3,4,5]
生成器與迭代器:
第一種方式通過(guò)括號(hào)的方式生成
生成器:()---(i for i in range(5)) ==>generator
這種一邊循環(huán)一邊計(jì)算的機(jī)制,稱(chēng)為生成器:generator。
生成器只有在調(diào)用時(shí)才會(huì)生成相應(yīng)的數(shù)據(jù),只記錄當(dāng)前位置。
只有一個(gè)__next__()方法
第二種方式通過(guò)yield生成
在函數(shù)中使用yield即可將一個(gè)函數(shù)變?yōu)橐粋€(gè)生成器
迭代器:
直接作用于for循環(huán)的數(shù)據(jù)類(lèi)型:
一類(lèi)是集合數(shù)據(jù)類(lèi)型,如list、tuple、dict、set、str等;
一類(lèi)是generator,包括生成器和帶yield的generator function。
直接作用于for循環(huán)的對(duì)象統(tǒng)稱(chēng)為可迭代對(duì)象:Iterable。
可以使用isinstance()判斷一個(gè)對(duì)象是否是Iterable對(duì)象
from collections import Iterable isinstance([], Iterable)=========true
*可以被next()函數(shù)調(diào)用并不斷返回下一個(gè)值的對(duì)象稱(chēng)為迭代器:Iterator。
可以使用isinstance()判斷一個(gè)對(duì)象是否是Iterator對(duì)象:
>>> from collections import Iterator>>> isinstance((x for x in range(10)), Iterator)======>True
生成器都是Iterator對(duì)象,但list、dict、str雖然是Iterable,卻不是Iterator。
把list、dict、str等Iterable變成Iterator可以使用iter()函數(shù):
例如:iter([])<====迭代器
Python的Iterator對(duì)象表示的是一個(gè)數(shù)據(jù)流,Iterator對(duì)象可以被next()函數(shù)調(diào)用并不斷返回下一個(gè)數(shù)據(jù),直到?jīng)]有數(shù)據(jù)時(shí)拋出StopIteration錯(cuò)誤。可以把這個(gè)數(shù)據(jù)流看做是一個(gè)有序序列,但我們卻不能提前知道序列的長(zhǎng)度,只能不斷通過(guò)next()函數(shù)實(shí)現(xiàn)按需計(jì)算下一個(gè)數(shù)據(jù),所以Iterator的計(jì)算是惰性的,只有在需要返回下一個(gè)數(shù)據(jù)時(shí)它才會(huì)計(jì)算。
Iterator甚至可以表示一個(gè)無(wú)限大的數(shù)據(jù)流,例如全體自然數(shù)。而使用list是永遠(yuǎn)不可能存儲(chǔ)全體自然數(shù)的。
小結(jié):
凡是可作用于for循環(huán)的對(duì)象都是Iterable類(lèi)型;
凡是可作用于next()函數(shù)的對(duì)象都是Iterator類(lèi)型,它們表示一個(gè)惰性計(jì)算的序列;
集合數(shù)據(jù)類(lèi)型如list、dict、str等是Iterable但不是Iterator,不過(guò)可以通過(guò)iter()函數(shù)獲得一個(gè)Iterator對(duì)象。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解2. UDDI FAQs3. PHP字符串前后字符或空格刪除方法介紹4. JSP之表單提交get和post的區(qū)別詳解及實(shí)例5. html清除浮動(dòng)的6種方法示例6. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容7. asp.net core項(xiàng)目授權(quán)流程詳解8. css進(jìn)階學(xué)習(xí) 選擇符9. PHP循環(huán)與分支知識(shí)點(diǎn)梳理10. ASP基礎(chǔ)入門(mén)第三篇(ASP腳本基礎(chǔ))
