forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.py
More file actions
25 lines (19 loc) · 687 Bytes
/
iterator.py
File metadata and controls
25 lines (19 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'''http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/'''
"""Implementation of the iterator pattern with a generator"""
def count_to(count):
"""Counts by word numbers, up to a maximum of five"""
numbers = ["one", "two", "three", "four", "five"]
# The zip keeps from counting over the limit
for number, pos in zip(numbers, list(range(count))):
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()
print('Counting to five...')
for number in count_to_five():
print(number, end=' ')
print()