How to make an empty generator?

Steve Holden steve at holdenweb.com
Thu Feb 18 22:52:13 EST 2010


Stephen Hansen wrote:
> On Thu, Feb 18, 2010 at 2:56 PM, Robert Kern <robert.kern at gmail.com
> <mailto:robert.kern at gmail.com>> 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.
> 
> Thanks.
> --S
> 
So add a comment to the yield statement so it reads

    yield # force empty generator

I realise that Jacob Kaplan-Moss calls comments in the code "lies", but
the reader really only needs a small clue.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list