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

Using iterator with next

A feature of any iterator is that we could iterate over it using the next call.

from counter import Counter

cnt = Counter()

while True:
    try:
        a = next(cnt)
        print(a)
    except Exception as ex:
        print(ex.__class__.__name__)
        break

Output:

1
2
3
StopIteration