Jim Jim - 2 months ago 9x
Python Question

Iterators for built-in containers

From my understanding so far, you can easily create an iterator for a user-defined object by simply defining both the

method and the
method for it. That's pretty intuitive to understand.

I also get it that you can manually build an iterator for any built-in container by simply calling the
method on that container.

Using basically any container as an example, what I don't understand is why they don't define a
method for themselves. Instead, when calling the
method on a container (ergo,
) it returns a new object of type
and not the container itself.

So finally, the question is, why do container objects delegate their
functionality to separate
objects instead of defining it themselves?


If the container was its own iterator (e.g. provided a __next__ method), you could only iterate over it in one place. You could not have independent iterators. Each call to __next__ would give the next value in the container and you'd not be able to go back to the first value; you have in effect a generator that could only ever yield the values in the container just the once.

By creating separate iterators for a given container, you can iterate independently:

>>> lst = ['foo', 'bar', 'baz']
>>> it1 = iter(lst)
>>> it2 = iter(lst)
>>> next(it1)
>>> next(it2)
>>> list(it1)
['bar', 'baz']
>>> next(it2)