[Python-ideas] try-else without except or finally

Steven D'Aprano steve at pearwood.info
Thu Nov 10 14:28:50 CET 2011


Yuval Greenfield wrote:

> "Try...else..." doesn't make any sense without the except jn my eyes. The
> "else" has to conjugate something.


We already have while...else and for...else blocks in Python. The 
principle is the same:


while condition():
     ...
else:
     # Run the else block unless we exit the while block with a break.
     ...


try:
     ...
else:
     # Run the else block unless we exit the try block with an exception.
     ...
finally:
     ...


This currently doesn't work without a pointless "except: raise" clause 
to satisfy the compiler. I try to avoid writing pointless code.



-- 
Steven




More information about the Python-ideas mailing list