Confused compare function :)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Dec 7 03:55:23 CET 2012


On Thu, 06 Dec 2012 13:51:29 +0000, Neil Cerutti wrote:

> On 2012-12-06, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> total = 0
>> for s in list_of_strings:
>>     try:
>>         total += int(s)
>>     except ValueError:
>>         pass  # Not a number, ignore it.
> 
> If it's internal data, perhaps. Of course, that would mean I had the
> option of *not* creating that stupid list_of_strings.


Not necessarily, it depends on the application.

If you have a spreadsheet, and create a formula =SUM(A1:A50) the expected 
behaviour is to just skip anything that is not a valid number, not raise 
an error. Sometimes correct application-level behaviour is to just ignore 
input that it doesn't care about.

One of the things that I used to despise with a passion about MYOB is 
that if you clicked on the window outside of a button or field, it would 
scream at you "ERROR ERROR ERROR - that was not a button or field!!!!" 
That is to say, it would beep. I mean, *who fscking cares* that the user 
clicks in the window background? It's a harmless tick, like tapping your 
desk, just ignore it.

As a general rule, library functions should be strict about reporting 
errors, while applications may be more forgiving about errors that they 
don't care about. The "don't care about" part is important though -- your 
word processor shouldn't care about low level networking errors, but it 
should care if it can't save a file to a network drive. 


-- 
Steven



More information about the Python-list mailing list