Python/Iterators

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Abstract

A brief overview of what iterators are and why they are useful.

What is an iterator in Python?

In Python, we don't worry too much about an object's class. We are concerned instead with its interface.

An iterator is an interface that iterates through some sequence or set of stuff. In Python, iterators only go one direction--forward. An iterable is any object that has a common-sense iterator associated with it.

In Python, we usually want to be able to get the iterator for an object. To do so, we use the iter function.

it = iter([1,2,3]) # -> iterator over the list [1,2,3]

The object that is passed to the iter function can either be an iterable or something else. An iterable provides the __iter__ method and returns an iterator. An iterator would simply return itself. Anything else would likely cause iter to raise a TypeError.

To get the next value in the iteration from the iterator, call the next method.

it = iter([1,2,3])
it.next() # -> 1
it.next() # -> 2
it.next() # -> 3

When the iteration is done, a StopIteration exception is raised.

it = iter([1,2,3])
it.next() # -> 1
it.next() # -> 2
it.next() # -> 3
it.next() # StopIteration raised

It's a pretty simply interface.

Where are iterators used?

Simply put, everywhere you can think they could possibly be used. This programming paradigm is so powerful that it really takes care of quite a few cases. For instance, for loops operate exclusively on iterables. The functions that deal with lists also take iterables.

You can also get iterators for objects that aren't the common-sense iterators. For instance, on a dict, you may want to iterate over the keys or the values, or both. There are three methods to give you an iterator for your particular choice.

You can also build iterators that use other iterators, building some kind of stream flow. This creates a very interesting programming paradigm useful for many problems.

How do I create my own iterator?

You can create your own custom iterators in any of these methods:

  1. Create a new object with an __iter__ and next methods.
  2. Use a generator.
  3. Use a generator comprehension.