How to make an empty generator?
robert.kern at gmail.com
Thu Feb 18 23:56:09 CET 2010
On 2010-02-18 16:25 PM, Stephen Hansen wrote:
> This has to be a stupid question, but :)
> I have some generators that do stuff, then start yielding results. On
> occasion, I don't want them to yield anything ever-- they're only really
> "generators" because I want to call them /as/ a generator as part of a
> generalized system.
> The only way I can figure out how to make an empty generator is:
> def gen():
> # do my one-time processing here
> Is there a better way? The return/yield just makes me flinch slightly. I
> tried just raising StopIteration at the end, but of course that didn't work.
def __init__(self, func, *args, **kwds):
self.func = func
self.args = args
self.kwds = kwds
Then write regular functions with the one-time processing code (not
generators!). When you go to pass them into your system that wants an iterator,
just wrap it with once(func).
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list