There are two terms when discussing generators.
# Generator function that produces 1 for the first time
# 2 second time and 3 third time
1 p >
# Driver code to test over generator function
1 2 3
1 2 3
For another example, here's a Fibonacci number generator.
0 1 1 2 3 Using for in loop 0 1 1 2 3
Applications: Suppose we are creating a stream of Fibonacci numbers, applying the approach generator makes it trivial; we just need to call next (x) to get the next Fibonacci number without worrying about where or when the stream of numbers ends.
A more practical type of stream handling — processing large data files such as log files. Generators provide an efficient way to do this because only portions of the file are processed at a time. We can also use Iterators for these purposes, but Generator provides a quick way (we don't need to write __next__ and __iter__ methods here).
Link below for more advanced generator applications in Python.
This article is courtesy of Shwetanshu Rohatgi . Please post comments if you find something wrong or if you would like to share more information on the topic under discussion