Back to search

Iterable vs iterator, custom iterators, generators, and `send`/`throw`/`close`

An iterable can produce an iterator, an iterator yields values one at a time, generators are a compact way to build iterators, and advanced generator methods can push values or exceptions back in.

Python Hard Theory

Iterable vs iterator, custom iterators, generators, and `send`/`throw`/`close`

An iterable can produce an iterator, an iterator yields values one at a time, generators are a compact way to build iterators, and advanced generator methods can push values or exceptions back in.

  • Iterables and iterators are not the same
  • Generators implement the iterator protocol for you
  • `send`, `throw`, and `close` control generator execution

Iterable vs iterator, custom iterators, generators, and `send`/`throw`/`close`