Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Python

Python using Yield and Generators

2022-07-19

Generators are memory efficient. They allow us to code with minimum intermediate arguments, less data structures.

Generators are of two types, generator expressions and generator functions.

Generator Expressions:

Consider:

gen_list = (i*i for i in xrange(3))
normal_list = [i*i for i in xrange(3)]
for g in gen_list:
    print g
print '------------'
for n in normal_list:
    print n
print '------------'
#again print gen_list
for g in gen_list:
    print g
print '------------'
#again print normal_list
for n in normal_list:
    print n
Output:
0
1
4
------------
0
1
4
------------
------------
0
1
4

As you can see we can use normal list multiple times, but generator comprehension can be used only once.
Generator expressions are useful when we are worried about memory efficiency.
However list comprehensions are much faster.

Use Case:
Suppose we have 10GB of text file, now lets open it and print each line.

with open('txt_file', 'r') as f:
    for line in f:
        print line

Now the above code will definitely kills your ram and hangs your system. Since 10GB file should not be read at once, instead we can break down the file into chunks and read. 
But to have less code we can use generators.

with open('txt_file', 'r') as f:
    lines = (line for line in f)

The above code just creates a generator object, it doesn't read any line until next method is called on it.
'for loop' calls the 'next' method at every iteration, so we dont have to explicitly mention it.

for line in lines:
    print line

There we go, we have the file contents and our system is happy.

Generator Functions:
Consider the following example:

def gen():
    yield n
    yield n+1
result = gen()
n = 0
print next(result)
print next(result)
Output:
0
1

gen() is a generator function, commonly referred as a generator.
when we call gen() the statements in the function are not executed, next(result) yields value n and suspends.
Again when we call next(result) it yields n+1 value and suspends.