Generator

say it: “JEN-uh-ray-ter” /ˈdʒɛnəreɪtər/

A function that hands out values one at a time with yield.

What is a generator?

A generator is a function that hands out values one at a time with yield, only when asked. It never builds the whole list, so it can work through huge amounts of data using very little memory.

def ticket_numbers(start):
    number = start
    while True:
        yield number
        number += 1

tickets = ticket_numbers(100)
print(next(tickets), next(tickets))  # 100 101

Level: Freelancer · More Python terms at this level

Updated