Boolean value of generators

Paul Rubin no.email at nospam.invalid
Fri Oct 15 04:08:57 CEST 2010


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> (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.

You wouldn't generate the cached value ahead of time.  You'd just
remember the last generated value so that you could use it again.
Sort of like getc/ungetc.

An intermediate measure might be to have a stdlib wrapper that added
caching like this to an arbitrary generator.  I've written such things a
few times in various clumsy ways.  Having the caching available in the C
code would eliminate a bunch of indirection.



More information about the Python-list mailing list