AI engineering glossary · Python advanced

What is iterator protocol?

Iterator protocol: The rule set that lets objects work in for loops: an iterable defines __iter__ returning an iterator, and the iterator defines __next__ to return each item and raise StopIteration at the end.

Level: advancedTopic: Python advanced

Iterator protocol: an example

A class with __iter__ returning self and __next__ counting from 1 to 5 can be used directly in for i in Countdown().

Related terms

More Python advanced terms

Where you learn it

Iterator protocol is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.