Python provides two different but related protocols called an
iterable. The Python Practice Book provides a good explanation, including complete examples. I highly recommend reading it and then coming back here. For the sake of completeness, I will layout the basics here as well.
Iterable – Definition
Any object that enables us to loop (or iterate) over it, by providing an iterator (directly or indirectly; more on that later). In other words, such that can be used in a for-each semantics:
for item in iterable: ...
Python standard collections are iterables (list, dict, set), file objects can also be used as iterables, and many more.
An iterable needs to comply with one of the following:
- Directly – implement an __iter__ method that returns an iterator (that comply to the protocol below).
- Indirectly – implement a __getitem__ method to return the item in a given index (or IndexError exception when the location is invalid. This method can be used to create a generic iterator.
You can get an iterable by using:
- Use built-in iterables, like list, dict, set and more.
- Create a class that complies with the protocol.
- Use a generator method.
- Some built-in or library methods will return an iterable object (e.g. xrange)
Iterator – Definition
An iterator is usually created from an iterable object (by invoking
Usually it isn’t the same instance, but another one the has a reference to its parent. It is used to iterate, or loop, over the iterable that provided it.
An iterator is an object that complies with the following interface:
- Every iterator is an iterable – so it needs to comply with the iterable protocol (implement __iter__ method, that usually returns itself).
- Implement a
nextmethod that return the next element or raise a StopIteration exception.
You can get an iterator by:
- Invoking iter(iterable) for any iterable.
- Create a class that complies with the protocol. Checkout zrange example here.
- Different libraries provides iterators, such as itertools.
This post is work-in-progress.
I intend to add more content soon, but for now, enjoy the overview.