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

Rob Cliffe rob.cliffe at btinternet.com
Mon Mar 21 19:53:03 EDT 2016


On 21/03/2016 18:37, Michel Desmoulin wrote:
>
> Le 21/03/2016 17:34, Chris Barker a écrit :
>> I've taught enough and I'm sure everyone else here has too, to know that
>> the "else" in a for loop in non-intuitive to a lot of folks. And maybe a
>> different keyword would have been clearer. But it is what it is, could
>> we keep this discussion to the proposed addition?
> +1 I would love "else" semantics to be changed as well, but we can't.
>
> What about:
>
> for x in stuff:
>      foo(x)
> or:
>      bar()
The trouble with "or" or "else" after a for-loop is that it suggests 
(following English usage) alternative actions: Either execute this 
for-loop, or do something else.
If I were designing Python 4, I might opt for

for x in stuff:
     foo(x)
ifnobreak:
     bar()

Pro: "ifnobreak" is at least explicit, and not particularly likely to 
clash with an already-used variable name.
Con: "ifnobreak" is too long and wordy.  But I can't think of anything 
better.  "aftercomplete", "whencomplete" are just as bad.
Hell, I'm coming round to "then".  What about allowing "then" to have an 
indent between that of the for-statement and the for-body:

for x in stuff():
                 foo(x)
         then:
                 bar()

Of course, you still have to learn it, like all idioms, but having 
learned it, isn't it a bit more readable?
The idea could be extended to other suites, conveying that everything 
indented is associated with the initial statement of the suite:

try:
                 foo()
         except SomeException:
                 bar()

Rob Cliffe



More information about the Python-ideas mailing list