Boolean value of generators

Carl Banks pavlovevidence at
Thu Oct 14 19:47:42 CEST 2010

On Oct 14, 6:36 am, Peter Otten <__pete... at> wrote:
> * I would recommend that you avoid the above approach. Pythonic solutions
> favour EAFP ( over look-
> before-you-leap:
> try:
>     value = next(y)
> except StopIteration:
>     print "ran out of values"
> else:
>     do_something_with(value)
> or
> value = next(y, default)

Good idea but not always convenient.  Sometimes you have to perform
some setup ahead of time if there are any items, and must not perform
that setup if the there are no items.  It's a PITA to use EAFP for
that, which is why an iterator that consumes and caches can be a
useful thing.

Carl Banks

More information about the Python-list mailing list