It's a follow-up to my 1 generator -- multiple consumers question. As StopIteration
is the way the generator signals its exhaustion, unfortunately, I now have many exception-handling code littered all over the place in the client
code (for every next()
statement in the example below).
Is there a better way to exit with whatever value is built in meal
upon hitting the first StopIteration
exception?
def client(course, take):
meal = []
for _ in range(take):
try:
some_meal = next(course)
meal.append(some_meal)
except StopIteration:
pass
if take % 2 == 0:
try:
some_meal = next(course)
meal.append(some_meal)
except StopIteration:
pass
return meal
UPDATE Eventually, I ended up using 'itertools.islice' (see accepted solution below) as this function takes care of the StopIteration
itself (see the for
-loop equivalent implementation shown in the itertools
doc. I prefer this solution over using next
default second argument as it would imply checking each meal
(still, I'd better use the latter than all the exception handling above).