Learn Steps

Generators in Python

Generators are just like normal functions, what makes functions generators in yield statement instead of the return statement. Also since you starting to read about generators, you should have a look at Iterators in python and how it works.
Lets read about generators in python.

What is yield?

Yield is a keyword that is used to return the value only, the difference is it is used to induce the lazy processing capability. What is does is it returns the state of the functions at that time and save it locally so that it can be resumed from there in the next iteration. So it actually doesn’t generate any value but save the function state so that it can be generated on the run time. Have a look at the code below

def testgen():
    for i in range(10000):
        yield i

So what is happening here is we have created a generator using yeild. Yeild creates an iterator by itself so it works out of the box from with for-in

In the above function, we have created a generator that is generating a list from 0,9999.

Advantages of generators in python

Generators in python have following advantages – They don’t process at the same time but does lazy loading so when the value is required it gets executed and gets the data. – They help a lot in saving memory by not generating and saving things in memory all at once.

Memory Efficient and lazy processing.

Let’s have a look at the code below.

from sys import getsizeof

def tesgen():
        for i in range(10000):
                yield i

b = [x for x in range(10000)]
print(getsizeof(b))

a = tesgen()
print(getsizeof(a))

If you can run this program you can see the difference between memory consumption in both the cases. Let’s discuss why there is so much difference in memroy consumption here.

Memory difference is because there is no actual calculation and storing of the values in the memory.

Infinite Sequence of data

It is easy to have an infinite sequence of data with generators because of the memory advantage otherwise for infinite sequence of data you need infinite RAM.

Iterator by default

Generators automatically define __iter__ and __next__. Thus these can be used directly with for-in and has iterators. You can get the iterators using iter keyword.

Python generator expression

a = (x**2 for x in my_list)

Above code creates a generator, what you need to do this is use () with your normal expression.

This was an introduction to generators. These are very powerful and you can use them to accomplish different tasks. Now it’s up to you how you will dig deeper and use it.

If you like the article please share and subscribe for more such articles.