[Python-Dev] PEP 340: Else clause for block statements

Nick Coghlan ncoghlan at iinet.net.au
Sun May 1 15:02:50 CEST 2005


As yet, I don't have a particularly firm opinion on whether or not block 
statements should support an 'else:' clause. And there are obviously a great 
many other questions to be answered about how block statements might work that 
are more important than this one.

Still, I've been tinkering with some ideas for how to approach this, and thought 
I'd write them up for everyone else's consideration.

Option 0:
    No else clause allowed. Figured I should mention this, since it is Guido's 
last reported inclination, and my total lack of use cases for the other options 
below suggests this is the best idea for an initial implementation.

Option 1: mimic try, for, while semantics
    An 'else' clause on a block statement behaves like the else clause on for 
and while loops, and on try/except statements - the clause is executed only if 
the managed suite completes 'normally' (i.e. it is not terminated early due to 
an exception, a break statement or a return statement)

Option 2: mimic if semantics
   An 'else' clause on a block statement behaves vaguely like the else clause on 
an if statement - the clause is executed only if the first suite is never 
entered, but no exception occurs (i.e. StopIteration is raised by the first call 
to next).

Option 3: iterator-controlled semantics
   The iterator is given the ability to control whether or not the else clause 
is executed (e.g. via an attribute of StopIteration), probably using option 1 
above as the default behaviour.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


More information about the Python-Dev mailing list