Generators
Generators
Generators
Generator...................................................................................................................................................1
Generator Function in Python.................................................................................................................1
Create a Generator in Python.................................................................................................................1
Generator Object......................................................................................................................................2
Python Generator Expression.................................................................................................................5
Generator Expression Syntax.................................................................................................................5
Applications of Generators in Python.....................................................................................................6
Generator
A Generator in Python is a function that returns an iterator using
the Yield keyword.
def function_name():
yield statement
In this example, we will create a simple generator that will yield
three integers. Then we will print these integers by using Python
for loop.
# A generator function that yields 1 for first time,
# 2 second time and 3 third time
def simpleGeneratorFun():
yield 1
yield 2
yield 3
Output:
1
2
3
Generator Object
Python Generator functions return a generator object that is
iterable, i.e., can be used as an Iterator. Generator objects are
used either by calling the next method of the generator object or
using the generator object in a “for in” loop.
Example:
In this example, we will create a simple generator function in
Python to generate objects using the next() function.
# A Python program to demonstrate use of
# generator object with next()
# A generator function
def simpleGeneratorFun():
yield 1
yield 2
yield 3
# x is a generator object
x = simpleGeneratorFun()
# In Python 3, __next__()
print(next(x))
print(next(x))
print(next(x))
Output:
1
2
3
Example:
In this example, we will create two generators for Fibonacci
Numbers, first a simple generator and second generator using a
for loop.
# A simple generator for Fibonacci Numbers
def fib(limit):
# Initialize first two Fibonacci Numbers
a, b = 0, 1
Output
0
1
1
2
3
for i in generator_exp:
print(i)
Output:
0
10
20
Applications of Generators in Python
Suppose we create a stream of Fibonacci numbers, adopting the
generator approach makes it trivial; we just have to call next(x) to
get the next Fibonacci number without bothering about where or
when the stream of numbers ends. A more practical type of
stream processing is handling large data files such as log files.
Generators provide a space-efficient method for such data
processing as only partsA of the file are handled at one given
point in 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).