Generator Question

GZ zyzhu2000 at gmail.com
Thu Dec 22 00:45:13 EST 2011


Hi,

I am wondering what would be the best way to return an iterator that
has zero items.

I just noticed the following two are different:

def f():
   pass
def g():
   if 0:  yield 0
    pass

for x in f(): print x
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
TypeError: 'NoneType' object is not iterable

for x in g(): print x
#loop exits without any errors

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?

Thanks,
gz








More information about the Python-list mailing list