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

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Aug 10 16:32:27 EDT 2006


Boris Borcic a écrit :
> 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.

While it's technically true - and I while I have a taste for compactness 
- skipping the explicit 'return False' somehow hurts my sense of 
esthethic... Unless you propose to return object or type  instead of 
True - but then it begins to look very strange !-)



More information about the Python-list mailing list