How to make an empty generator?

Stephen Hansen apt.shansen at
Fri Feb 19 00:36:02 CET 2010

On Thu, Feb 18, 2010 at 2:56 PM, Robert Kern <robert.kern at> wrote:

> class once(object):
>    def __init__(self, func, *args, **kwds):
>        self.func = func
>        self.args = args
>        self.kwds = kwds
>    def __iter__(self):
>        return self
>    def next(self):
>        self.func(*self.args, **self.kwds)
>        raise StopIteration()

Hmm, yeah. I'd probably tweak it into a decorator and name it
sideeffect_only or something, but yeah, that's the right approach at least.

My motivation is clarity, I can just see a colleague a year from now asking
me, "... what the hell is return / yield?" and although this is more
expensive, its less clear to me.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list