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

Sven R. Kunze srkunze at mail.de
Wed Mar 23 17:30:15 EDT 2016


On 23.03.2016 19:19, Michael Selik wrote:
>> 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
>>
>> Maybe I'm missing something, but this seems to be an unnecessary addition.
> It’s comparable in purpose to the ``else`` clause on a ``for`` or ``while``. It eliminates the need for a flag variable, like ``was_completed`` or ``is_empty``. It saves two lines of code, but more importantly avoids the accidental flip of True/False when setting the flag variable. Whether that warrants adding new syntax... I’m not convinced by the current proposals, but I can imagine finding a good keyword for the purpose. A keyword as good as ``else`` at least.

Good point.

Best,
Sven


More information about the Python-ideas mailing list