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.
print(next(a)) # 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.