访问一个数据容器的内容而无需暴露它的内部表示,支持对该数据容器的多种遍历
# -*- coding: utf-8 -*-
def count_to(count):
# 1.内部以列表形式存储 该数据
_numbers = ["one", "two", "three", "four", "five"]
# 2.内部以 IO形式 及时请求数据
_numbers = ["one", "two", "three", "four", "five"]
for pos, number in zip(range(count), _numbers):
yield number
# Test the generator
count_to_two = lambda: count_to(2)
count_to_five = lambda: count_to(5)
print('Counting to two...')
for number in count_to_two():
print(number, end=' ')
print('Counting to five...')
for number in count_to_five():
print(number, end=' ')