裝飾器
'''
增加裝飾時間子產品
1、不修改原有函數内容
2、不修改函數調用方式
import time
def timer(func):
def deco():
start_time=time.time()
func()
stop_time=time.time()
print 'running time:',stop_time-start_time
return deco()
@timer
def test1():
time.sleep(3)
print 'int the test1'
增加認證子產品
user,passwd='alex','abc123'
def auth(auth_type):
def outer_wrapper(func):
def wrapper(*args,**kwargs):
if auth_type=='local':
username=raw_input('username:')
password=raw_input('password:')
if user==username and password==passwd:
print '\033[32;1mUser has passed authentication\033[0m'
res=func(*args,**kwargs)
return res
else:
exit('\033[31;1mInvalid username or password\033[0m')
elif auth_type=='ldap':
print 'no ldap'
return wrapper
return outer_wrapper
@auth(auth_type='local')
def index():
print 'in the index'
return 'from index'
@auth(auth_type='ldap')
def home():
print 'in the home'
return 'from home'
index()
home()
疊代器
疊代器就是有一個next()方法的對象。
疊代器也有一些限制,例如不能向後移動,不能回到開始,也不能複制一個疊代器。如果要再次或同時疊代同個對象,隻能去建立另一個疊代器對象。
reversed()内建函數将傳回一個反序列通路的疊代器。
enumerate()内建函數同樣也傳回疊代器。
此外,python還提供一整個itertools子產品,它包含各種有用的疊代器。
如:
myTuple=(123,'xyz',45.67)
i=iter(myTuple)
i.next()
如果是一個實際應用程式,那麼我們需要把代碼放在一個try-except塊中。序列現在會自動地産生它們自己的疊代器,是以一個for循環:
for i in seq:
do_something_to(i)
實際上是這樣工作的:
fetch=iter(seq)
while True:
try:
i=fetch.next()
except StopIteration:
break
do_something_to(i)
除了序列之外,字典和檔案是另外兩個可疊代的python資料類型。
生成器
協同程式是可以運作的獨立函數調用,可以暫停或者挂起,并從程式離開的地方繼續或者重新開始。
挂起傳回出中間值并多次繼續的協同程式被稱為生成器,那就是python的生成器真正做的事。
什麼是python式的生成器?從文法上講,生成器是一個帶yield語句的函數。一個函數或者子程式隻傳回一次,但一個生成器能暫停執行并傳回一個中間的結果——那就是yield語句的功能,傳回一個值給調用者并暫停執行。當生成器的next()方法被調用的時候,它會準确地從離開地方繼續。
python的for循環有next()調用和對StopIteration的處理。
如果想終結一個生成器,可以調用close()方法。
本文轉自Grodd51CTO部落格,原文連結:http://blog.51cto.com/juispan/1960330,如需轉載請自行聯系原作者