Saltar a contenido

Chapter 24: Generators

Generators are functions that return an iterable set of items, one at a time, using yield.

Example

Python
1
2
3
4
5
6
7
8
def count_up_to(max):
    count = 1
    while count <= max:
        yield count
        count += 1

for number in count_up_to(5):
    print(number)