Alex Rivera | Logout

How do I know if a generator is empty from the start?

Asked 2009-03-19T09:51:24.130
249

Is there a simple way of testing if the generator has no items, like peek, hasNext, isEmpty, something along those lines?

Edit
Report

1 Answer

1

If you need to know before you use the generator, then no, there is no simple way. If you can wait until after you have used the generator, there is a simple way:

was_empty = True

for some_item in some_generator:
    was_empty = False
    do_something_with(some_item)

if was_empty:
    handle_already_empty_generator_case()
answered 2011-11-01T19:47:29.997

Your Answer