Boolean value of generators

Grant Edwards invalid at invalid.invalid
Fri Oct 15 10:54:28 EDT 2010


On 2010-10-14, Paul Rubin <no.email at nospam.invalid> wrote:
> Carl Banks <pavlovevidence 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,

That would play havoc with generators that had side effects.  Not
everybody who writes generators is using them to generate Fibonacci
numbers.

> so you could test for emptiness, look ahead at the next item without
> consuming it, etc.

And what happens when the generator is doing things like executing
database transactions?

> 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.


-- 
Grant Edwards               grant.b.edwards        Yow! Excuse me, but didn't
                                  at               I tell you there's NO HOPE
                              gmail.com            for the survival of OFFSET
                                                   PRINTING?



More information about the Python-list mailing list