From my understanding so far, you can easily create an iterator for a user-defined object by simply defining both the
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) 'foo' >>> next(it2) 'foo' >>> list(it1) ['bar', 'baz'] >>> next(it2) 'bar'