Skip to main content

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 # Use the generator. x = generator(5) # Now calling the decorator print("Calling generator:") x.next() print("Calling generator:") x.next() print("Calling generator:") x.next() success_msg("Great job!")



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.

Comments

Popular posts from this blog

Immutable vs Mutable Data structures.

Majorly used Data Structures in python are as follows: List Set Tuple Dictionary String Numbers(int, float, decimal) One of the major things in python discussed is whether the data structure is mutable or immutable. What do this terms mean? In simple terms, Immutable means value cannot be changed after it is assigned to a variable. Whereas, In  mutable data type value can be changed after assignment. Hmm, What ??? This is still not clear these are merely dictionary definitions I am blabbering. Amazon.in Widgets