Generator Question

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Dec 22 00:52:25 EST 2011


On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote:

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

return iter([])


> I just noticed the following two are different:
> 
> def f():
>    pass

That creates a function that does nothing, and then returns None (because 
Python doesn't have Pascal procedures or C void function).


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

The pass is redundant.

This creates a generator function which, when called, doesn't yield 
anything, then raises StopIteration.



-- 
Steven



More information about the Python-list mailing list