max(), sum(), next()

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Sep 4 03:30:14 CEST 2008


On Wed, 03 Sep 2008 16:20:39 -0700, Mensanator wrote:

>>>> sum([])
> 0
> 
> is a bug, just as it's a bug in Excel to evaluate blank cells as 0. It
> should return None or throw an exception like sum([None,1]) does.

You're wrong, because 99.9% of the time when users leave a blank cell in 
Excel, they want it to be treated as zero. Spreadsheet sum() is not the 
same as mathematician's sum, which doesn't have a concept of "blank 
cells". (But if it did, it would treat them as zero, since that's the 
only useful thing and mathematicians are just as much pragmatists as 
spreadsheet users.) The Excel code does the right thing, and your "pure" 
solution would do the unwanted and unexpected thing and is therefore 
buggy.

Bugs are defined by "does the code do what the user wants it to do?", not 
"is it mathematically pure?". The current behaviour of sum([]) does the 
right thing for the 99% of the time when users expect an integer. And the 
rest of the time, they have to specify a starting value for the sum 
anyway, and so sum([], initial_value) does the right thing *always*.

The only time it does the wrong thing[1] is when you forget to pass an 
initial value but expect a non-numeric result. And that's the 
programmer's error, not a function bug.





[1] I believe it also does the wrong thing by refusing to sum strings, 
but that's another story.


-- 
Steven



More information about the Python-list mailing list