-
Notifications
You must be signed in to change notification settings - Fork 22
/
samplegenerators.py
47 lines (39 loc) · 1.28 KB
/
samplegenerators.py
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
# Generators hold values that are fetched lazily.
# Meaning that the entire collection isn't stored
# in memory all at once, but rather retrieved when
# needed.
# This is best used when dealing with a large
# collection of items. Such as rows returned from
# a database call or looping over a large csv.
# Generators are intelligent enough to handle these
# large collections without running out of memory.
# They dispose of variables in memory that are no
# longer used and do not worry about variables
# that are not yet needed.
# Here's the syntax for a generator. It's just a
# function! Take note of the yield keyword. yield
# basically means 'return', but lets Python know
# we'll be coming back for more.
def color_generator():
yield 'blue'
yield 'orange'
yield 'yellow'
yield 'purple'
# One way to use generators is by calling `next()`
# on it's instance
g = color_generator() # create the instance
next(g) # 'blue'
next(g) # 'orange'
next(g) # 'yellow'
next(g) # 'purple'
# However, once a generator is exhausted, it will
# not start back at the beginning.
next(g) # Raises StopIteration error.
# They're also iterables. No StopIteration errors
# are thrown with this method.
for color in color_generator():
print(color)
# 'blue'
# 'orange'
# 'yellow'
# 'purple'