Boolean value of generators

Carl Banks pavlovevidence at gmail.com
Thu Oct 14 18:53:55 EDT 2010


On Oct 14, 10:53 am, Paul Rubin <no.em... at nospam.invalid> wrote:
> Carl Banks <pavlovevide... at gmail.com> writes:
> > In general, the only way to test if a generator is empty is to try to
> > consume an item.  (It's possible to write an iterator that consumes an
> > item and caches it to be returned on the next next(), and whose
> > boolean status indicates if there's an item left. ...)
>
> I remember thinking that Python would be better off if all generators
> automatically cached an item, so you could test for emptiness, look
> ahead at the next item without consuming it, etc.  This might have been
> a good change to make in Python 3.0 (it would have broken compatibility
> with 2.x) but it's too late now.

Since the generator's behavior can depend on when it was invoked, no I
don't think this is a good idea.

Carl Banks



More information about the Python-list mailing list