"also" to balance "else" ?
rrr at ronadam.com
Tue Jun 14 02:00:39 CEST 2005
There seems to be a fair amount of discussion concerning flow control
enhancements lately. with, do and dowhile, case, etc... So here's my
flow control suggestion. ;-)
It occurred to me (a few weeks ago while trying to find the best way to
form a if-elif-else block, that on a very general level, an 'also'
statement might be useful. So I was wondering what others would think
'also' would be the opposite of 'else' in meaning. And in the case of a
for-else... would correct what I believe to be an inconsistency.
Currently the else block in a for loop gets executed if the loop is
completed, which seems backwards to me. I would expect the else to
complete if the loop was broken out of. That seems more constant with
if's else block executing when if's condition is false.
'also' would work like this.
for x in <iteriable>:
if <condition>: break # do else block
where the also block is executed if the loop completes (as the current
else does), and the else block is executed if it doesn't.
if <condition2>: break # jump to else
Here if the while loop ends at the while <condition1>, the BLOCK2
executes, or if the break is executed, BLOCK3 executes.
In and if statement...
Here, the also block would execute if any previous condition is true,
else the else block would execute.
And 'tentatively'<g>, there is the possible 'alif', (also-if), to be
used this way.
break # skip to also block
also: # at lease one condition was true
else: # no conditions evaluated as true
Where only one elif will ever evaluate as true, any or all 'alif's could
evaluate as true. This works similar to some case statements except all
alif conditions may be evaluated. Not a true case statement, but a good
replacement where speed isn't the main concern?
I'm not sure if 'elif's and 'alif's could or should be used together?
Maybe a precedence rule would be needed. Or simple that all also's and
alif's must precede any elif's. Then again maybe alif's between elif's
could be useful? For example the alif <condition2> could be and 'elif'
instead, so the break above would't be needed.
'also' can be used without the else in the above examples, and of course
the 'else' doesn't need an 'also'.
I think this gives Pythons general flow control some nice symmetrical
and consistent characteristics that seem very appealing to me. Anyone
Any reason why this would be a bad idea? Is there an equivalent to an
also in any other language? (I haven't seen it before.)
Changing the for-else is probably a problem... This might be a 3.0 wish
list item because of that.
Is alif too simular to elif?
On the plus side, I think this contributes to the pseudocode character
of Python very well.
More information about the Python-list