[Python-Dev] Re: Reiterability

Guido van Rossum guido at python.org
Wed Oct 22 11:02:09 EDT 2003


> > I thought we already established before that attempting to guess wihch
> > parts of a generator function to copy and which parts to share is
> > hopeless.  generator-made iterators won't be __copy__-able, period.
> 
> > I think this is the weakness of this cloning business, because it
> > either makes generators second-class iterators, or it makes cloning a
> > precarious thing to attempt when generators are used.  (You can make a
> > non-cloneable iterator cloneable by wrapping it into something that
> > buffers just those items that are still reacheable by clones, but this
> > can still require arbitrary amounts of buffer space.
> 
> However, the buffering can be done in a way that uses only as much
> buffer space as is truly needed.  Just maintain the buffer as a
> singly linked list in which new elements are inserted at the *tail*
> of the list.  Then whenever the head becomes unreachable
> (e.g. because no iterators refer to it), it will be garbage
> collected.

Correct.  For this reason, Raymond will make a leak-proof version of
his tee() function part of itertools.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list