How the yield statement and the generator function work in Python?
A python function with a yield statement is called a generator function.
Generator functions has yield expressions in their function body that produce a series of values in a for loop or one value at a time while calling the __next__() method.
What a generator function generates?
- When a generator function is invoked no code in the generator function is executed.
- It simply returns a generator iterator object.
Each time the __next__() method of a generator iterator object is called - the generator function is executed
Till the yield statement
or
- return statement
or
End of the generator function
The moment the yield statement in the generator function is executed the state of the function is maintained and the value of the expression list is
returned to the caller - the caller who called the __next__() on generator iterator object.
Think of yield statement as just another external call to a function.
After returning the expression list the execution of the generator function continues till a return, end of the function.
As per Python's iterator protocol, each invocation of __next__() should return one value from the iterable/collection - which is what we get from the iterator object obtained from the generator function.
Example
# Write a generator function for a set of positive integers starting from zero
def PositiveInts(n):
i = 0
while i < n:
yield i
i = i +1
# Obtain a generator object
generatorObject = PositiveInts(2)
# call next() on generator object every time you need a value from the generator function
print(generatorObject.__next__())
print(generatorObject.__next__())
print("======================")
# for calls next() internally on the generator object
for i in PositiveInts(4):
print(i)Output
0
1
======================
0
1
2
3