FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-patterns/iterator.py at master · Pythoning/python-patterns · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Pythoning
/
python-patterns
Public
forked from
faif/python-patterns
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-patterns
/
iterator.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
28 lines (20 loc) · 777 Bytes
Breadcrumbs
python-patterns
/
iterator.py
Copy path
File metadata and controls
28 lines (20 loc) · 777 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
26
27
28
"""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"
]
# enumerate() returns a tuple containing a count (from start which
# defaults to 0) and the values obtained from iterating over sequence
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
()
print
(
'Counting to five...'
)
for
number
in
count_to_five
():
print
(
number
,
end
=
' '
)
print
()
Back
|
FazBrowse Home
|
New Git URL