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
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...
FABRIC – LEARNING PART 2 Ashwin Kumar

Using Fabric, we can develop interactive script for ec2 region, ec2 flavour amazon web services. For this, you need aws account, security group, key pair, ...

Continue Reading...
Publishing Python Modules with PIP via PyPi Ashwin Kumar

We'll install so many packages in our day to day python development. Now in this blog post, we'll try to know how to create our ...

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