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

generator, python

Solution

The simple answer to your question: no, there is no simple way. There are a whole lot of work-arounds.

There really shouldn't be a simple way, because of what generators are: a way to output a sequence of values without holding the sequence in memory. So there's no backward traversal.

You could write a has_next function or maybe even slap it on to a generator as a method with a fancy decorator if you wanted to.

Problem

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

Original source

Related problems