21.2 Iterators
You have often come across the term iterable object while reading this book, but so far you only know that you can iterate such instances with a for loop, for example, or pass them as parameters to certain functions, such as list. In this section, we’ll now look at the background and functionality of these objects.
Data types that combine multiple elements in one instance, such as lists, sets, or dictionaries, are referred to as containers. An iterator is an abstraction layer that allows enumerating the elements of a container through a standardized interface.
This abstracts from the concrete peculiarities of the container to access its elements so that any iterable object can be traversed with the same code. It’s then no longer ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access