Python GeneratorYield Keyword

time to read 1 min | 158 words

The yield keyword is fundamental to the creation of generators. Consider the following generator function:

def createGenerator():
    print('Initial call')
    yield '1'
    print('Second call')
    yield '2'

a = createGenerator()

Calling the createGenerator() function will create a generator object stored as a. Note that the code inside the generator function will not be run yet.

# Initial call
# 1

The first time the generator object is iterated over (in a loop or with next()), the function code will be run from the start until the first yield. The value in the yield statement is returned and the current position in the code is saved internally.

# Second call
# 2

The second next call will resume the code from just after the previous yield and will continue running it until another yield is found where it returns the desired value.

When there are no more yield keywords, the generator object is considered empty.

print(next(a)) # StopIteration error