Can't copy iterator object

Kerim Borchaev ( WarKiD ) warkid at storm.ru
Fri Oct 26 10:17:38 CEST 2001


Hello sebastien,

   2001-10-23 03:39:03 PST Michael Hudson explained:
>> Why iterators can't be copied?
> 
> Think about 
> 
>>>> i = iter(sys.stdin)
>>>> j = iter(i)
> 
> when sys.stdin is attached to a pipe.
> 
> or iter(iter(g())) for some generator g.
> 
> It just ain't possible in general.

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?

Best regards,
 Kerim                            mailto:warkid at storm.ru

Friday, October 26, 2001, 11:06:54 AM, you wrote:

>>  I'm not supposed to do so? This could be useful...
>> 
>> Python 2.2b1 (#25, Oct 19 2001, 11:44:52) [MSC 32 bit (Intel)] on win32
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> i = iter([1,2,3])
>> >>> import copy
>> >>> copy.copy(i)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in ?
>>   File "C:\Python22\lib\copy.py", line 84, in copy
>>     y = _reconstruct(x, reductor(), 0)
>>   File "C:\Python22\lib\copy_reg.py", line 56, in _reduce
>>     state = base(self)
>> TypeError: cannot create 'iterator' instances  

s> I had posted the same question few days before: here are the answers
s> http://groups.google.com/groups?hl=en&frame=right&th=6499660bd77fd7fc&seekm=7da8d8f8.0110230006.36933486%40posting.google.com#link1

s> Maybe a potential new entry in the FAQ?






More information about the Python-list mailing list