Generators in Python
Last Updated :
06 Sep, 2023
A Generator in Python is a function that returns an iterator using the Yield keyword. In this article, we will discuss how the generator function works in Python.
Generator Function in Python
A generator function in Python is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a Python generator function.
Create a Generator in Python
In Python, we can create a generator function by simply using the def keyword and the yield keyword. The generator has the following syntax in Python:
def function_name():
yield statement
Example:
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.
Python3
def simpleGeneratorFun():
yield 1
yield 2
yield 3
for value in simpleGeneratorFun():
print (value)
|
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.
Python3
def simpleGeneratorFun():
yield 1
yield 2
yield 3
x = simpleGeneratorFun()
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.
Python3
def fib(limit):
a, b = 0 , 1
while a < limit:
yield a
a, b = b, a + b
x = fib( 5 )
print ( next (x))
print ( next (x))
print ( next (x))
print ( next (x))
print ( next (x))
print ( "\nUsing for in loop" )
for i in fib( 5 ):
print (i)
|
Output:
0
1
1
2
3
Using for in loop
0
1
1
2
3
Python Generator Expression
In Python, generator expression is another way of writing the generator function. It uses the Python list comprehension technique but instead of storing the elements in a list in memory, it creates generator objects.
Generator Expression Syntax
The generator expression in Python has the following Syntax:
(expression for item in iterable)
Example:
In this example, we will create a generator object that will print the multiples of 5 between the range of 0 to 5 which are also divisible by 2.
Python3
generator_exp = (i * 5 for i in range ( 5 ) if i % 2 = = 0 )
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 parts 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).
Please Login to comment...