[Python-Dev] Chaining try statements: eltry?

Ron Adam rrr at ronadam.com
Fri Jul 8 04:23:36 CEST 2005


Guido van Rossum wrote:

> I even wonder if else-clauses on for/while were a good idea. (The one
> on try is definitely a good idea since the use case is quite frequent
> and only clumsily handled otherwise; the use cases for else on
> for/while are less convincing IMO.)


I'm +1 on removing the else from the loops myself.


The else's acts somewhat different depending on weather it's on an if, 
try, for, or while loop.  So it's four somewhat different behaviors for 
one keyword.

1. With an if, if you think in terms of flow testing you get the same 
results as if you look at it in in terms of value testing.  Either the 
if-block will execute or the else-block will, but never any part of one 
and then the other.  A binary solution to a binary problem.

2. With a try, It's a value test for no exception. Or you could consider 
it flow test for the try block completing.

3. In a while loop, it's a value test, where the else block gets 
executed if the while condition evaluates as false, the while block may 
or may not execute. You still need a flag to test for that.

4. In a for loop, it's a test of the iterator completing and generating 
a StopIteration exception.  Which is somewhat implicit.  The for block 
may or may not execute.


For Python 3000 it might be worth taking a look at flow control tests 
instead of value tests.

With loop flow control testing you have three possible values:

     None:  loop not started
     False: loop started but not completed, ie.. break
     True:  loop completed naturally

     * The current else behavior is an (if not False) test here.

I'm not sure how to test for the condition in a simple way. So it's 
still a -1 at this point.  <shrug>


As for elif...  it works for me. :)

Cheers,
Ron













More information about the Python-Dev mailing list