When working with generators, AFAIK, there's currently no easy way to handle 
the case of an empty generator (which can be useful if such case is an error).

Converting the generator to a, say, list, is not a solution if the generator is 
intrinsically infinite.

I propose to have an "otherwise" clause in the "for" statement that runs only 
if no items are to be processed; that is, to have the following code:

# where get_items() may be an infinite generator
for item in get_items():
    SUITE1
otherwise:
    SUITE2

be semantically equivalent to:

items_iter = iter(get_items())
try:
    first_item = next(items_iter)
except StopIteration:
    SUITE2
else:
    for item in itertools.chain([first_item], items_iter):
        SUITE1
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7ELTFTLTOCUDYPDK5PMTGLYUVD7UYOXW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to