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

Sven R. Kunze srkunze at mail.de
Tue Mar 22 08:20:05 EDT 2016


On 21.03.2016 02:44, Steven D'Aprano wrote:
> On Sun, Mar 20, 2016 at 07:12:58PM +0100, Sven R. Kunze wrote:
>
>> for item in my_iterator:
>>       # do per item
>> empty:
>>       # do something else
> "empty" is a poor choice, as I expect that it will break a lot of code
> that already uses it as a variable.
>
> if empty:
>     ...

So, "empty" is off the table I suppose.


>
>
>> *Keyword*
>> Keywords under consideration have been in (an attempt of dispassionate)
>> order of preference:
>> 1) empty -> most obvious, most people responded with a solution to solve
>> the intended problem
>> 2) else -> already taken but to some would be the preferred one
>> 3) or -> as alternative if a new keyword is too much
> I'm not too keen on the look of "or" for this:
>
> for x in iterable:
>      block
> or:
>      alternative
>
>
> but I think it's the least worst of the proposed keywords.

Got it but I am open for suggestions. :-)

>
>> 4) except -> as alternative if a new keyword is too much
> I think that "except" completely fails to describe what the keyword
> does, and it will give some people the misunderstanding that it catches
> errors in the body of the for-loop.


Now, that you mention it. This might give birth to a completely 
different idea. What about?


for item in collection:
     # do for item
except EmptyCollection:
     # do if collection is empty
except StopIteration:
     # do after the loop

Which basically merges try and for?

Best,
Sven



More information about the Python-ideas mailing list