How to make an empty generator?
Robert Kern
robert.kern at gmail.com
Thu Feb 18 19:12:10 EST 2010
On 2010-02-18 17:33 PM, Ben Finney wrote:
> Robert Kern<robert.kern at gmail.com> writes:
>
>> On 2010-02-18 16:25 PM, Stephen Hansen wrote:
>>> The only way I can figure out how to make an empty generator is:
>>>
>>> def gen():
>>> # do my one-time processing here
>>>
>>> return
>>> yield
>>>
>>> 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.
>
> No need to define functions or classes; let a generator expression take
> care of it for you::
>
> >>> foo = (x for x in list())
> >>> foo.next()
> Traceback (most recent call last):
> File "<stdin>", line 1, in<module>
> StopIteration
He doesn't want *any* empty generator. He wants an iterator that executes some
given side-effect-producing code then immediately raises the StopIteration.
--
Robert Kern
"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
mailing list