[Python-Dev] copying of itertools iterators (original) (raw)

Terry Reedy tjreedy at udel.edu
Fri Apr 2 01:52:43 CEST 2010


On 4/1/2010 7:20 PM, Andrew Svetlov wrote:

using of copy.copy for simple iterators is forbidden

import copy copy.copy(iter([1, 2, 3])) Traceback (most recent call last): File "", line 1, in File "/home/andrew/projects/py3k/Lib/copy.py", line 96, in copy return reconstruct(x, rv, 0) File "/home/andrew/projects/py3k/Lib/copy.py", line 284, in reconstruct y = callable(*args) File "/home/andrew/projects/py3k/Lib/copyreg.py", line 88, in newobj return cls.new(cls, *args) TypeError: object.new(listiterator) is not safe, use listiterator.new()

The same happens for the iterators of other builtin classes: tuples, sets, and dicts (that I tried). In the other hand,

copy.copy(iter(range(1,3,1))) # 3.1 Traceback (most recent call last): ... TypeError: rangeiter() requires 3 int arguments and similar for filter and map.

I do not know whether the former group is detected by rule or explicit hard-coded list, but I suspect the latter.

That behavior is safe and clean. But it's possible to copy iterator objects returned by itertools functions:

i = itertools.chain([1, 2], [3, 4, 5]) i.next() 1 j = copy.copy(i)

This works because itertools.chain() is legal

j.next() Traceback (most recent call last): File "", line 1, in StopIteration

Because itertools.chain() is empty.

i.next() 2 Looks like itertools object should be protected from usage like that.

I suspect only those for which itertools.xxx() works rather than raising an exception.

Folks, what are you think about?

Why privilige the itertools module? A possible rule would be to not copy anything with both .iter and .next.

Terry Jan Reedy



More information about the Python-Dev mailing list