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

Chris Angelico rosuav at gmail.com
Sat Jul 23 19:48:44 EDT 2016


On Sun, Jul 24, 2016 at 2:57 AM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> On 22.07.16 18:48, Chris Angelico wrote:
>>
>> 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.
>
>
> There is such a protocol. Use copy.copy().

Time machine strikes again! Restarting of iterators exists.

ChrisA


More information about the Python-ideas mailing list