[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: How to make an empty generator?

Steve Holden

2/19/2010 3:52:00 AM

Stephen Hansen wrote:
> On Thu, Feb 18, 2010 at 2:56 PM, Robert Kern <robert.kern@gmail.com
> <mailto:robert.kern@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....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...