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

Joao S. O. Bueno jsbueno at python.org.br
Wed Mar 23 15:06:25 EDT 2016


On 23 March 2016 at 14:13, Henshaw, Andy <Andy.Henshaw at gtri.gatech.edu> wrote:
>> -----Original Message-----
>>
>> 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
>
> is_empty = True
> for item in iterable:
>     is_empty = False
>     # do for each item
> if is_empty:
>     # do exactly when iterable raises StopIteration on the first

For me, after checking all other e-mails, this is the
"one and obvious way" of doing it, does not matter if it "seems" boring -
it had not to do with being boring, or having one extra variable - it
has to do with being readable.

Event the variant that would do

item = sentinel = object()
for ...

is less readable, if more elegant.

As for adding different syntax for this,
I am +0

For the idea of adding "except"  clauses to
'with', 'for'  and 'while' (along with a "EmptyIteration" exception)
I am + 0.5


>
> Maybe I'm missing something, but this seems to be an unnecessary addition.
>
> Andy
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list