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

Steven D'Aprano steve at pearwood.info
Sat Jul 23 23:07:07 EDT 2016


On Sun, Jul 24, 2016 at 09:48:44AM +1000, Chris Angelico wrote:
> On Sun, Jul 24, 2016 at 2:57 AM, Serhiy Storchaka <storchaka at gmail.com> wrote:

> > There is such a protocol. Use copy.copy().
> 
> Time machine strikes again! Restarting of iterators exists.

You can copy *some* iterators, but it doesn't restart them.

py> import copy
py> it = iter([1, 2, 3])
py> next(it)
1
py> next(it)
2
py> a = copy.copy(it)
py> next(a)
3
py> next(it)
3


I wouldn't expect that copying an iterator would, in general, restart 
it. In general, there's no way to restart an iterator. Once seen, values 
are gone and cannot be recreated.


-- 
Steve


More information about the Python-ideas mailing list