[Python-Dev] copying of itertools iterators (original) (raw)
Andrew Svetlov andrew.svetlov at gmail.com
Fri Apr 2 01:20:33 CEST 2010
- Previous message: [Python-Dev] Proposing PEP 376
- Next message: [Python-Dev] copying of itertools iterators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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(list_iterator) is not safe, use list_iterator.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?
- Previous message: [Python-Dev] Proposing PEP 376
- Next message: [Python-Dev] copying of itertools iterators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]