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
Getting Started with the IPython Notebook Shirisha Gaddi

IPython is a set of tools developed to make it easier for the programmers to work with Python and data. IPython provides extensions to the ...

Continue Reading...
Create excel file, Insert image, Draw Bar Graphs in excel files in python using xlsxwriter Chaitanya Kattineni

Xlsxwriter is a python module through which we can write data to Excel 2007+ XLSX file format. In this blog post we learn to write ...

Continue Reading...
Programming with python: Decorators Ashwin Kumar

Python decorators supports aspect-oriented programming. It is used to add or modify code in functions or classes. Using decorators will provide security, tracing, looking ..etc ...

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