"also" to balance "else" ?

Ron Adam 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 
of it.

'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>:
    BLOCK1
    if <condition>: break   # do else block
also:
    BLOCK2
else:
    BLOCK3

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.


while <condition1>:
     BLOCK1
     if <condition2>: break    # jump to else
also:
     BLOCK2
else:
     BLOCK3

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...

if <condition1>:
     BLOCK1
elif <condition2>:
     BLOCK2
elif <condition3>:
     BLOCK3
also:
     BLOCK4
else:
     BLOCK5

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.

if <condition1>:
     BLOCK1
alif <condition2>:
     BLOCK2
     break         # skip to also block
alif <condition3>:
     BLOCK3
alif <condition4>:
     BLOCK4
also:             # at lease one condition was true
     BLOCK5
else:             # no conditions evaluated as true
     BLOCK6

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 
agree?

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.

Cheers, Ron




More information about the Python-list mailing list