[Python-Dev] [Python-checkins] cpython (3.2): Nudge readers towards a more accurate mental model for loop else clauses
Terry Reedy
tjreedy at udel.edu
Thu Jun 7 20:08:47 CEST 2012
On 6/7/2012 8:42 AM, nick.coghlan wrote:
> 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
More information about the Python-Dev
mailing list