-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathgenerator.py
More file actions
32 lines (20 loc) Β· 773 Bytes
/
Copy pathgenerator.py
File metadata and controls
32 lines (20 loc) Β· 773 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
26
27
28
29
"""
It allows a client to have sequential access to the elements of an aggregate object (without exposing its underlying structure).
Composite related to iterator.
"""
def count_to(count):
"""Our iterator implementation"""
# Our list
numbers_in_german = ["eins", "zwei", "drei", "vier", "funf"]
# Our built-in iterator
# Creates a tuple such as (1, "eins")
iterator = zip(range(count), numbers_in_german)
# Iterate through our iterable list
# Extract the German numbers
# Put them in a generator called number
for position, number in iterator:
# Returns a 'generator' containing numbers in German
yield number
# Let's test the generator returned by our iterator
for num in count_to(7):
print(num)