[Python-ideas] copyable iterators, named loops, continue a expression per indentation and a few functional things
brett at python.org
Sun Dec 24 21:18:26 CET 2006
On 12/24/06, Talin <talin at acm.org> wrote:
> Calvin Spealman wrote:
> > 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.
> The OP already stated that the proposal is not meant to apply to
> generators, and rightly so, because there's no way to copy all of the
> mutable state that a generator might access.
> However, I think that the idea of copying 'iterators but not generators'
> is too restrictive, since externally you might not be able to tell the
> difference. That's why I suggested tee, since it works on both. No, you
> don't get the same side effects, but that's not what the OP was asking for.
Ditto from me. Generators are just a type of iterator technically, so
the OP really wanted to say "non-generator iterators". But that is a
More information about the Python-ideas