Iterators for embedded containers

From my understanding so far, you can easily create an iterator for a user-defined object by simply specifying both the method __iter__and the method __next__for it. This is pretty intuitive.

I also understand that you can manually create an iterator for any built-in container by simply calling a method iter()in that container.

Using basically any container as an example, I don’t understand why they don’t define a method __next__for themselves. Instead, when calling a method iterin a container (ergo, <container>.__iter__), it returns a new type object <container_type>_iterator, not the container itself.


So the question is, why do container objects delegate their functions iteratorto separate objects <type>_iteratorinstead of defining them?

+4
source share
1 answer

If the container was its own iterator (for example, provided __next__), you could iterate over it only in one place. You would not have independent iterators. Each call __next__will give the next value in the container, and you cannot return to the first value; you have a generator that can only ever give values ​​in a container only 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'
+9

Source: https://habr.com/ru/post/1612044/


All Articles