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
Introduction to Object Oriented Programming with Python Anjaneyulu Batta

Introduction to Object Oriented Programming with Python

Continue Reading...
Customize and Embed Vimeo Videos using Python Requests. Dinesh Deshmukh

Using python requests and vimeo endpoints it becomes very easy and simple to upload our videos and customize them.

Vimeo Access token:
1. Create ...

Continue Reading...
Generating PDF Files in Python using xhtml2pdf Siva Chittamuru

There are many ways for generating PDF in python. In this post I will be presenting PDF files generation with xhtml2pdf.

xhtml2pdf: xhtml2pdf is a ...

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