"""
迭代器模式:提供一種方法順序訪問一個聚合對象中的各個元素亮垫,而又不暴露該對象的內(nèi)部表示猛铅。
一個生成器實現(xiàn)迭代器模式
"""
from?__future__?import?print_function
def?count_to(count):
????"""由數(shù)字編號計數(shù)秀仲,最多五個"""
????numbers?=?["one",?"two",?"three",?"four",?"five"]
????#?枚舉()返回一個包含計數(shù)的元組(默認從0開始)從遍歷序列獲得值
????for?pos,?number?in?zip(range(count),?numbers):
????????yield?number
#?測試生成器
count_to_two?=?lambda:?count_to(2)
count_to_five?=?lambda:?count_to(5)
print('數(shù)到2...')
for?number?in?count_to_two():
????print(number,?end='?')
print()
print('數(shù)到5...')
for?number?in?count_to_five():
????print(number,?end='?')
print()