Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generators - call next

We can also use a for loop on the generator and then we don't need to worry about the exception.

def number():
    yield 42
    yield 19
    yield 23

num = number()
print(type(num))
for n in num:
    print(n)

Output:

<class 'generator'>
42
19
23