[Python-Dev] copying of itertools iterators (original) (raw)
Raymond Hettinger raymond.hettinger at gmail.com
Fri Apr 2 01:50:08 CEST 2010
- Previous message: [Python-Dev] copying of itertools iterators
- Next message: [Python-Dev] copying of itertools iterators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Apr 1, 2010, at 4: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() 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) j.next() Traceback (most recent call last): File "", line 1, in StopIteration i.next() 2 Looks like itertools object should be protected from usage like that. Folks, what are you think about?
I find it hard to get excited about this. It doesn't seem to have been a problem in the real world (no complaints, bug reports, or feature requests).
The tee() itertool is the official way to split-out an iterator stream -- it is also copyable with copy.copy().
The itertools.count() function is also copyable. Running copy.copy() on other itertools is currently undefined (though I may add copy support to itertools.repeat() and the combinatoric functions).
Also, I seems to me the copy.copy() is itself not very bright about what it tries to copy and in giving clear messages about whether or not it successfully made a copy.
Raymond
- Previous message: [Python-Dev] copying of itertools iterators
- Next message: [Python-Dev] copying of itertools iterators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]