[Python-ideas] Control Flow - Never Executed Loop Body
Michel Desmoulin
desmoulinmichel at gmail.com
Mon Mar 21 20:03:45 EDT 2016
Le 22/03/2016 00:53, Rob Cliffe a écrit :
>
>
> 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.
But that's exactly the goal of "or" here. Run only if the loop is never
executed since stuff is empty.
> If I were designing Python 4, I might opt for
>
> for x in stuff:
> foo(x)
> ifnobreak:
> bar()
Python 4 won't break compat the way Python 3 did (Guido's words), so
Python 4 is not a better opportunity for introducing new stuff than any
other release.
But I do like the "nobreak" better than else (you can drop the if, it's
just noise). However, again, we can't remove else, and "there should be
one way to do it" prevents us to add duplicates keywords.
>
> 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
>
> _______________________________________________
> 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