Python using yield and generators.

Reading Time : ~ .

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.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Python Coding Techniques and Programming Practices Anjaneyulu Batta

Coding techniques and programming practices are one of the features of a professional programmer. While writing code to solve a problem programmer should make simple ...

Continue Reading...
Python to Debian package: Simple, Easy and Fast Dinesh Deshmukh

Packaging python script to debian follows strict instructions, using the following instructions, most of the steps can be skipped hence making it easy and fast.
Continue Reading...

Working with python collections part 2 Rakesh babu Podishetty

Python collections - Counter is to count the frequency of character, OrderedDict is to track the order of the contents in which they are added ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties