Can't copy iterator object

Tim Peters tim.one at home.com
Fri Oct 26 15:49:26 EDT 2001


[Kerim Borchaev]
> I can agree that one shouldn't be able to copy "non-copiable" iterator.
> Like file iterator. But why not leave it to the particular iterator
> implementation? So as file's iterator to be "non-copiable" but list's
> one - copiable?

It *is* up to each particular iterator implementation, and you can add any
methods you like to your own iterators.  All of the builtin iterators chose
not to support copying, due to a lack of interest, compelling use cases, and
time.  Note that "copy" is also ambiguous along several dimensions:  besides
the usual "shallow vs deep" distinction, it's also unclear whether
iterator.copy() "should" in general strive to duplicate the *current* state
of the iterator, or to return a fresh restart-from-the-beginning iterator
"over the same object".  The latter meaning (sometimes called "refresh") may
be more useful more often.  Regardless, the builtin iterators chose to stay
out of these arguments entirely for now.  That doesn't preclude them from
growing new behaviors in later releases, provided someone writes a
compelling PEP in favor of some.





More information about the Python-list mailing list