How to make an empty generator?
Stephen Hansen
apt.shansen at gmail.com
Thu Feb 18 18:36:02 EST 2010
On Thu, Feb 18, 2010 at 2:56 PM, Robert Kern <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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100218/b55d4f2c/attachment-0001.html>
More information about the Python-list
mailing list