[Python-ideas] copyable iterators, named loops, continue a expression per indentation and a few functional things

Calvin Spealman ironfroggy at gmail.com
Sun Dec 24 17:20:33 CET 2006

On 12/24/06, Talin <talin at acm.org> wrote:
> Josiah Carlson wrote:
> > Mathias Panzenböck <grosser.meister.morti at gmx.net> wrote:
> >> Copyable iterators
> >> ------------------
> >>
> >> There are a few ways how you could implement this. However, it only makes sense for iterators, not
> >> for generators!
> >
> > -1 on the entire proposal.  All iterators (and generators) are already
> > copyable.  It's called list().
> Actually, what he wants to be able to do is to 'tee' an iterator.
> However, we already have a 'tee' function in itertools that does just
> that. (And it works on generators too!)
> -- Talin

If I am understanding the proposal correctly, which I think I am,
itertools.tee is not the same as what this is proposing. itertools.tee
simply iterates the original and caches the results to be given when
iterating over the two tees created for it. This is not the same as
copying an iterator. Copying an iterator would create a new iterator
of the same state as the original. For example, with generators the
copy would be a new generator object with the same state as the
original and iterating over the copy would execute the same code as
the first.

def g():
    i = 0
    while True:
        print i
        yield i
        i = i + 1

If you "copy" this with itertools.tee, you only see i printed once for
each iteration of the generator, not each iteration of the tees. With
a real copying of iterators, if I copied this, iterating over the
copies would always produce an output of that copies value of i.

Read my blog! I depend on your acceptance of my opinion! I am interesting!

More information about the Python-ideas mailing list