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

Terry Reedy tjreedy at udel.edu
Wed Mar 23 18:25:55 EDT 2016


On 3/22/2016 3:34 PM, Ethan Furman wrote:
> On 03/22/2016 11:49 AM, Terry Reedy wrote:

>> I have never, ever, claimed that.  Initially empty and empty after all
>> items have been processed are different.
>
> This is the heart of the issue: *initially empty* and *empty after all
> items are exhausted* are different,

from the perspective of a particular loop.

> but for/else and while/else treat them the same.

Which is to say, each test is a test of current state, independent of 
history.  It does not matter if the iterater was created empty, was 
emptied by previous code out or inside the current function, or emptied 
by the current loop.  People who want to condition on history should 
record it.  The idiom presented previously is a form of recording history.

item = marker = object()  # could be earlier in function body
for item in iterable:
     ...
else:
     if item = marker:  # started loop empty
         ...

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list