Codementor Events

Generators in Python

Published Nov 01, 2018
Generators in Python

One of the most commonly asked question in Python interviews is "Have you worked with generators? How and what was the need to use it?"

This post will try to explore what are they how and why they are used?

Basically, Generator in simple terms can be thought as the function which returns series of results instead of one single result.

Syntactic difference between function and generator is, Generator uses "yield" keyword instead of "return" keyword.

Generators give us an iterator(Something which we can loop on for ex. list or dictionary) so they can be used with looping constructs such as 'for' and 'while' loops.

Example:

Define a generator.

def generator(number):
  while True: 
    	number += 1 
        yield number 

The difference with Function:

So again basically generators give us an iterator which we can loop over.
They are memory efficient too. Because normal function would do all the processing, store the result in memory and then return final one time at the end.

But in generators results are calculated and accessed as and when required one at a time.

One thing to note here is "yield" keyword we can define our own Generator class as well. This can be done by implementing __iter__ and __next__ methods of a class.

Usage:

According to me, Usage of generators is where we want a continuous stream of data to be processed sequentially for ex. reading file contents, generating a long stream of numbers such as prime numbers, odd/even numbers etc.

You should always check if you can use a generator for a particular requirement which involves looping over data. Thanks.

Please share to support if you like my work.

Discover and read more posts from Kishan S Mehta
get started
post commentsBe the first to share your opinion
Show more replies