
On Fri, Jul 22, 2016 at 8:48 AM, Chris Angelico <rosuav@gmail.com> wrote:
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).
Sequences don't give you this *guarantee* either. A trivial example: class MyList(list): def __getitem__(self, ndx): # In "real world" this might be meaningful condition and update if random() < .333: if isinstance(ndx, slice): for n in range(ndx.start, ndx.stop, ndx.step or 1): self[n] += 1 else: self[ndx] += 1 return super().__getitem__(ndx)
What you might be looking at is a protocol for "bookmarking" or "forking" an iterator. That might be more useful. For example:
How is this different from itertools.tee()? -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th.