The yield statement suspends a function’s execution
and sends a value back to the caller,but retains enough state to enable the function to resume
where it left off. When the function resumes, it continues execution immediately after
the last yield run. This allows its code to produce a series of values over time, rather than
computing them at once and sending them back like a list.
Let’s see with an example:
# A Simple Python program to demonstrate working
# of yield
# A generator function that yields 1 for the first time,
# 2 second time and 3 third time
def simpleGeneratorFun():
yield 1
yield 2
yield 3
# Driver code to check above generator function
for value in simpleGeneratorFun():
print(value)
OUTPUT
1
2
3
# A Python program to generate squares
from 1 to 100 using yield and therefore generator
# An infinite generator function that prints
# next square number. It starts with 1
def nextSquare():
i = 1
# An Infinite loop to generate squares
while True:
yield i*i
i += 1 # Next execution resumes
# from this point
# Driver code to test above generator
# function
for num in nextSquare():
if num > 100:
break
print(num)
OUTPUT
1
4
9
16
25
36
49
64
81
100
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()
# Iterating over the generator object using next
# 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
# One by one yield next Fibonacci Number
while a < limit:
yield a
a, b = b, a + b
# Create a generator object
x = fib(5)
# Iterating over the generator object using next
# In Python 3, __next__()
print(next(x))
print(next(x))
print(next(x))
print(next(x))
print(next(x))
# Iterating over the generator object using for
# in loop.
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.
# generator expression
generator_exp = (i * 5 for i in range(5) if i%2==0)
for i in generator_exp:
print(i)