converting a nested try/except statement into try/except/else
John Salerno
johnjsal at NOSPAMgmail.com
Thu Aug 10 16:49:08 EDT 2006
Boris Borcic wrote:
> Slawomir Nowaczyk wrote:
>>
>> try:
>> if int(text) > 0:
>> return True
>> except ValueError:
>> pass
>> self.error_message()
>> return False
>>
>
> Nicely DRY. To make it even more compact, it may be noticed that the
> default return value None is false in a boolean context - so that the
> last line is superfluous if the return value is only wanted to test it
> in such a context.
In this case the method must return False, because it's a wxPython
method that needs a True or False value. If it doesn't, the program will
continue even after the error message.
What I did to make it compact was have the error_message() method return
False, so I can just call
return self.error_message()
More information about the Python-list
mailing list