[Python-Dev] Chaining try statements: eltry?

Nick Coghlan ncoghlan at gmail.com
Fri Jul 8 11:11:25 CEST 2005


Phillip J. Eby wrote:
> At 02:48 PM 7/7/2005 -0400, Tim Peters wrote:
>>I also suspect that if they weren't in the language already, a PEP to
>>introduce them would fail, because
>>
>>    still_looking = True
>>    some loop:
>>        if found it:
>>            still_looking = False
>>            break
>>    if still_looking:
>>        # what would have been in the "else" clause
>>
>>is clear and easy to write without it.
> 
> 
> *shudder* Okay, you just convinced me.  "Else" should stay, because the 
> above is much less readable and writable!

I think Aahz's point is a good one - conditional flow control can be most 
clearly represented using try blocks:

   class BreakException(Exception): pass

   try:
       some loop:
           if found it:
               raise BreakException
   except BreakException:
       pass
   else:
       # What would have been in the else clause

Defining 'else' on loops as short-hand for the above may make the intent of 
the clauses clearer (that is, for/else and while/else are more closely related 
to try statements than they are to if statements).

Regards,
Nick.

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


More information about the Python-Dev mailing list