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

Boris Borcic bborcic at gmail.com
Thu Aug 10 16:05:13 EDT 2006


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.



More information about the Python-list mailing list