Generator Question

GZ zyzhu2000 at gmail.com
Sat Dec 24 16:19:44 EST 2011


I see. Thanks for the clarification.

On Dec 22, 12:35 am, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> 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 ...:yieldx
>
> > In other words, when certain condition is met, I want toyieldnothing.
> > How to do?
>
> Actually, there's an even easier way.
>
> >>> def h():
>
> ...     if not condition:
> ...         for c in "abc":
> ...            yieldc
> ...
>
> >>> condition = False
> >>> list(h())
> ['a', 'b', 'c']
> >>> condition = True
> >>> list(h())
>
> []
>
> --
> Steven
>
>




More information about the Python-list mailing list