Generator Question
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Dec 22 01:35:30 EST 2011
On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote:
> Now the question here is this:
>
> def h():
> if condition=true:
> #I would like to return an itereator with zero length
> else:
> for ...: yield x
>
> In other words, when certain condition is met, I want to yield nothing.
> How to do?
Actually, there's an even easier way.
>>> def h():
... if not condition:
... for c in "abc":
... yield c
...
>>>
>>> condition = False
>>> list(h())
['a', 'b', 'c']
>>> condition = True
>>> list(h())
[]
--
Steven
More information about the Python-list
mailing list