converting a nested try/except statement into try/except/else

John Salerno johnjsal at NOSPAMgmail.com
Wed Aug 9 14:51:04 EDT 2006


I'm starting out with this:

try:
     if int(text) > 0:
         return True
     else:
         self.error_message()
         return False
except ValueError:
     self.error_message()
     return False

I rewrote it as this:

try:
     int(text)
except ValueError:
     self.error_message()
     return False
else:
     return True

I think it's much cleaner, but obviously I lost the test in the original 
if statement.

So my question is, can I still retain this second structure and still 
test for > 0, but not have any extra nesting?

Thanks.



More information about the Python-list mailing list