[Python-Dev] [Python-checkins] cpython (3.2): Nudge readers towards a more accurate mental model for loop else clauses

Nick Coghlan ncoghlan at gmail.com
Fri Jun 8 00:05:22 CEST 2012


The inaccuracies in the analogy are why this is in the tutorial, not the
language reference. All 3 else clauses are really their own thing. For if
statements, the full construct is "if/elif/else", for loops it is
"for/break/else" and "while/break/else" and for try statements it is
"try/except/else". Early returns and uncaught exceptions will skip any of
the three.

However, emphasising the link to if statements has been demonstrably
confusing for years, due to the interpretation of empty iterables as False
in a boolean context. The new text is intended to emphasise that, no, the
for loop else clause does *not* map directly to an if statement that checks
that iterable.

--
Sent from my phone, thus the relative brevity :)
On Jun 8, 2012 5:12 AM, "Terry Reedy" <tjreedy at udel.edu> wrote:

>
>
> On 6/7/2012 8:42 AM, nick.coghlan wrote:
>
>> http://hg.python.org/cpython/**rev/6e4ec47fba6a<http://hg.python.org/cpython/rev/6e4ec47fba6a>
>> changeset:   77369:6e4ec47fba6a
>> branch:      3.2
>> parent:      77363:aa9cfeea07ad
>> user:        Nick Coghlan<ncoghlan at gmail.com>
>> date:        Thu Jun 07 22:41:34 2012 +1000
>> summary:
>>   Nudge readers towards a more accurate mental model for loop else clauses
>>
>> files:
>>   Doc/tutorial/controlflow.rst |  7 +++++++
>>   1 files changed, 7 insertions(+), 0 deletions(-)
>>
>>
>> diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst
>> --- a/Doc/tutorial/controlflow.rst
>> +++ b/Doc/tutorial/controlflow.rst
>> @@ -187,6 +187,13 @@
>>  (Yes, this is the correct code.  Look closely: the ``else`` clause
>> belongs to
>>  the :keyword:`for` loop, **not** the :keyword:`if` statement.)
>>
>> +When used with a loop, the ``else`` clause has more in common with the
>> +``else`` clause of a :keyword:`try` statement than it does that of
>> +:keyword:`if` statements: a :keyword:`try` statement's ``else`` clause
>> runs
>> +when no exception occurs,
>>
>
> (And there no return. But that is true of of every statement.)
>
> I think the above is wrong.
>
> I claim that try-statement else: is essentially identical to if-statement
> else:. The try-statement else: clause is subordinate to the except:
> clauses, not the try: part itself. It must follow at least one except
> condition just as an if-statement must follow at least one if condition. So
> it is really an except else, not a try else.
>
> Furthermore, 'except SomeError' is an abbreviation for 'if/elif SomeError
> was raised since the corresponding try' and more particularly 'if/elif
> isinstance(__exception__, SomeError). I use __exception__ here as the
> equivalent of C's errno, except that it is hidden. (If it were not hidden,
> we would not need 'as e', and indeed, we would not really need 'except'
> either.) The else clause runs when all the implied conditionals of the
> excepts are false. Just as an if-statement else clause runs when all the
> explicit conditionals of the if/elifs are false.
>
> The real contrast is between if/except else and loop else. The latter is
> subordinate to exactly one condition instead of possibly many, but that one
> condition may be evaluated multiple times instead of just once. It is the
> latter fact that seems to confuse people.
>
>  and a loop's ``else`` clause runs when no ``break`` occurs.
>>
>
> As I explained on Python-ideas, the else clause runs when the loop
> condition is false. Period. This sentence is an incomplete equivalent to
> 'the loop condition is false'. The else clause runs when no 'break', no
> 'return', no 'raise' (explicit or implicit), AND no infinite loop occurs.
>
> I think your addition should be reverted. Here is a possible alternative
> if you think something more is needed.
>
> "An else clause used with a loop has two differences from an else clause
> used with an if statement. It is subordinate to just one condition instead
> of possibly many. (In for statements, the condition is implicit but still
> there.) That one condition is tested repeatedly instead of just once. A
> loop else is the same in that it triggers when that one condition is false."
>
> ---
> Terry Jan Reedy
>
> ______________________________**_________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/**mailman/listinfo/python-dev<http://mail.python.org/mailman/listinfo/python-dev>
> Unsubscribe: http://mail.python.org/**mailman/options/python-dev/**
> ncoghlan%40gmail.com<http://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120608/1fb09424/attachment.html>


More information about the Python-Dev mailing list