Boolean value of generators

Steven D'Aprano steve at
Fri Oct 15 03:48:06 CEST 2010

On Thu, 14 Oct 2010 14:13:30 -0500, Tim Chase wrote:

>> 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.
> Generators can do dangerous things...I'm not sure I'd *want* to have
> Python implicitly cache generators without an explicit wrapper to
> request it:

I'm sure that I DON'T want it. It would be a terrible change.

(1) Generators are lightweight. Adding overhead to cache the next value 
adds value only for a small number of uses, but adds weight to all 

(2) Generators are simple. There is a clear and obvious distinction 
between "create the generator object by calling the generator function" 
and "call the generated values by iterating over the generator object". 
Admittedly the language is a bit clumsy, but the concept is simple -- you 
have a generator function that you call, and it returns an iterable 
object that yields values. Simple and straightforward. Caching blurs this 
distinction -- calling the function also produces the first object, 
caching it and hiding any StopIteration.

(3) Generators with side-effects. I know, I know, if you write functions 
with side-effects, you're in a state of sin already, but there's no need 
for Python to make it worse.

(4) Expensive generators. The beauty of generators is that they produce 
values on demand. Making all generators cache their first value means 
that you pay that cost even if you end up never needing the first value.

(5) Time dependent output of generators. The values yielded can depend on 
the time at which you invoke the generator. Caching plays havoc with that.

None of this is meant to say "Never cache generator output", that would 
be a silly thing to say. If you need an iterator with look-ahead, that 
knows whether it is empty or not, go right ahead and use one. But don't 
try to force it on everyone.


More information about the Python-list mailing list