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

David Mertz mertz at gnosis.cx
Fri Jul 22 13:58:36 EDT 2016


On Fri, Jul 22, 2016 at 8:48 AM, Chris Angelico <rosuav at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160722/d0ea9e65/attachment-0001.html>


More information about the Python-ideas mailing list