FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-patterns/patterns/behavioral/iterator.py at master · botdigit/python-patterns · GitHub
botdigit
/
python-patterns
Public
forked from
faif/python-patterns
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-patterns
/
patterns
/
behavioral
/
iterator.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (36 loc) · 918 Bytes
Breadcrumbs
python-patterns
/
patterns
/
behavioral
/
iterator.py
Copy path
File metadata and controls
48 lines (36 loc) · 918 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
Implementation of the iterator pattern with a generator
*TL;DR80
Traverses a container and accesses the container's elements.
"""
from
__future__
import
print_function
def
count_to
(
count
):
"""Counts by word numbers, up to a maximum of five"""
numbers
=
[
"one"
,
"two"
,
"three"
,
"four"
,
"five"
]
for
number
in
numbers
[:
count
]:
yield
number
# Test the generator
count_to_two
=
lambda
:
count_to
(
2
)
count_to_five
=
lambda
:
count_to
(
5
)
def
main
():
"""
# Counting to two...
>>> for number in count_to_two():
... print(number)
one
two
# Counting to five...
>>> for number in count_to_five():
... print(number)
one
two
three
four
five
"""
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
Back
|
FazBrowse Home
|
New Git URL