What is the use of Generator in Python?

We can use Generator to create Iterators in Python.

A Generator is written like a regular function.

It can make use yield statement to return data during the function call.

In this way we can write complex logic that works as an Iterator.

A Generator is more compact than an Iterator due to the fact that iter() and next() functions are automatically created in a Generator.

Also within a Generator code, local variables and execution state are saved between multiple calls.

Therefore, there is no need to add extra variables like self.index etc to keep track of iteration.

Generator also increases the readability of the code written in Python. It is a very simple implementation of an Iterator.


Posted

in

Tags:

Comments

Leave a Reply