[Python-ideas] An ABC representing "Iterable, Sized, Container"

Chris Angelico rosuav at gmail.com
Fri Jul 22 11:48:34 EDT 2016


On Sat, Jul 23, 2016 at 12:20 AM, Daniel Moisset
<dmoisset at machinalis.com> wrote:
> Would there be interest in some kind of method/API for restarting iterators?
> If there was a it.restart() (or reset(it)), it would probably be a good idea
> to name the concept in the same way as the method ( "Restartable" or
> "Resettable"). And having that as an actual protocol would simplify the
> discussion on both what the type should contain and how it should be named,
> while having actal applications to simplify code (and reduce memory usage in
> cases when creating a list is not needed, just making multiple passes).

IMO, no. Some iterators can be restarted by going back to the original
iterable and requesting another iterator, but with no guarantee that
it will result in the exact same sequence (eg dict/set iterators).
Does that count as restarting? And what if you start by consuming a
header or two, then pass the resulting iterator to another function.
Based on type, it would be restartable (say it's a list_iterator), but
the called function would be highly surprised to suddenly get back
more results.

What you might be looking at is a protocol for "bookmarking" or
"forking" an iterator. That might be more useful. For example:

# Like itertools.cycle()
def cycle(it):
    it = iter(it)
    while True:
        yield from fork(it)
        # and then, in effect, "restart" it

With list_iterator, forking is straight-forward - make another
iterator off the same list, at the same point this one is. Compared to
the normal implementation of itertools.cycle, this wouldn't need any
auxiliary storage, as it'd just reference the base list every time.
It's kinda like restarting the iterator, but if you give this a
partly-consumed list_iterator, it would cycle through the remaining
elements only. (How many iterators would be forkable, I don't know,
but I expect it'd be the exact same ones that would be restartable by
any other definition.)

ChrisA


More information about the Python-ideas mailing list