23

The Iterator Pattern

The Iterator pattern is one of the simplest and most frequently used of the design patterns. It enables you to move through a list or collection of data using a standard interface without having to know the details of the internal representations of that data. In addition, you can define special iterators that perform some special processing and return only specified elements of the data collection.

Why We Use Iterators

The Iterator provides a defined way to move through a set of data elements without exposing what is taking place inside the class. Because the Iterator is an interface, you can implement it in any way that is convenient for the data you are returning. Design Patterns suggests that a suitable interface ...

Get Python Programming with Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.