Generators

Generators and yield

If a function uses the yield keyword, it defines an object known as a generator.

The primary use of a generator is to produce values for use in iteration. Here's an example:

def countdown(n):
    print('Counting down from', n)
    while n > 0:
        yield n
        n -= 1

# Example use
from x in countdown(10):
    print('T-minus', x)

Restartable Generators

Generator Delegation

Using Generators in Practice

Enhanced Generators and yield Expressions

Applications of Enhanced Generators

Generators and the Bridge to Awaiting

Last updated