[Python-ideas] Control Flow - Never Executed Loop Body

Chris Angelico rosuav at gmail.com
Mon Mar 21 04:06:29 EDT 2016


On Mon, Mar 21, 2016 at 7:01 PM, Stephen J. Turnbull <stephen at xemacs.org> wrote:
>     item = _sentinel = object()
>     for item in iterable:
>         # do for each item
>     if item is _sentinel:
>         # do exactly when iterable raises StopIteration on the first pass

What if 'iterable' is locals().values()? Can you, with perfect
reliability, recognize that case? AIUI this is exactly why next() and
__next__() are defined to "return a value or raise", rather than
"return a value or return a magic no-more-values value", because
there's always the possibility that the no-more-values value is a
legitimately-yielded value.

Maybe this situation isn't important enough or common enough to
justify dedicated syntax, but it's definitely a possibility.

ChrisA


More information about the Python-ideas mailing list