The Iterator pattern
In programming, we use sequences or collections of objects a lot, particularly in algorithms and when writing programs that manipulate data. One can think of automation scripts, APIs, data-driven apps, and other domains. In this chapter, we are going to see a pattern that is useful whenever we must handle collections of objects: the Iterator pattern.
Note, according to the definition given by Wikipedia
Iterator is a design pattern in which an iterator is used to traverse a container and access the container’s elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.
The Iterator pattern is extensively used in the Python context. As we will see, this translates into Iterator being a language feature. It is so useful that the language developers decided to make it a feature.
Use cases for the Iterator pattern
It is a good idea to use the Iterator...