Generators in Python - GeeksforGeeks

31 Mar, 2020
Prerequisites: Yield Keyword and Iterators
There are two terms involved when we discuss generators.
Generator-Function : A generator-function 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 generator function.# A generator function that yields 1 for first time,# 2 second time and 3 third timedef simpleGeneratorFun():    yield 1                yield 2                yield 3               # Driver code to check above generator functionfor value in simpleGeneratorFun():     print(value)Output :1
2
3Generator-Object : Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop (as shown in the above program).# A Python program to demonstrate use of # generator object with next()   # A generator functiondef simpleGeneratorFun():    yield 1    yield 2    yield 3   # x is a generator objectx = simpleGeneratorFun()  # Iterating over the generator object using nextprint(x.next()) # In Python 3, __next__()print(x.next())print(x.next())Output :1
2
3
Generator-Function : A generator-function 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 generator function.# A generator function that yields 1 for first time,# 2 second time and 3 third timedef simpleGeneratorFun():    yield 1                yield 2                yield 3               # Driver code to check above generator functionfor value in simpleGeneratorFun():     print(value)Output :1
2
3
# A generator function that yields 1 for first time,# 2 second time and 3 third timedef simpleGeneratorFun():    yield 1                yield 2                yield 3               # Driver code to check above generator functionfor value in simpleGeneratorFun():     print(value)
Output :
1
2
3
Generator-Object : Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop (as shown in the above program).# A Python program to demonstrate use of # generator object with next()   # A generator functiondef simpleGeneratorFun():    yield 1    yield 2    yield 3   # x is a generator objectx = simpleGeneratorFun()  # Iterating over the generator object using nextprint(x.next()) # In Python 3, __next__()print(x.next())print(x.next())Output :1
2
3
# A Python program to demonstrate use of # generator object with next()   # A generator functiondef simpleGeneratorFun():    yield 1    yield 2    yield 3   # x is a generator objectx = simpleGeneratorFun()  # Iterating over the generator object using nextprint(x.next()) # In Python 3, __next__()print(x.next())print(x.next())
Output :
1
2
3
So a generator function returns an generator object that is iterable, i.e., can be used as an Iterators .
As another example, below is a generator for Fibonacci Numbers.
# A simple generator for Fibonacci Numbersdef 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 objectx = fib(5)  # Iterating over the generator object using nextprint(x.next()) # In Python 3, __next__()print(x.next())print(x.next())print(x.next())print(x.next())  # 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
Applications : Suppose we to 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).
Refer below link for more advanced applications of generators in Python.http://www.dabeaz.com/finalgenerator/
This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
fridayda13
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()