From the course: Python: Design Patterns

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Iterator

Iterator

- The iterator pattern allows a client to have sequential access to the elements of an aggregate object without exposing its underlying structure. The problem is that some programmers overcrowd the traversal interfaces of an aggregate object for every possible way of iteration. We'll be building our own iterator that takes advantage of a built in Python iterator called zip. The iterator goes through a list of German counting words. It will iterate only up to a certain point based on client input. The iterator isolates access and traversal features from the aggregate object. It also provides an interface for accessing the elements of an aggregate object. An iterator keeps track of the objects being traversed. Our solution is to make the aggregate object create an iterator for a client. The composite design pattern is related to the iterator pattern.

Contents