Generators

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Contents

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.

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
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

# Driver code to check above generator function


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.
# 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)

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).

You might also like