max(), sum(), next()

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Sep 7 00:05:40 EDT 2008


On Sat, 06 Sep 2008 11:22:07 -0700, Mensanator wrote:

[...]

>> They could have decided that sum must take at least two arguments,
>> because addition requires two arguments and it's meaningless to talk
>> about adding a single number without talking about adding it to
>> something else. But they didn't.
> 
> Ok. But the problem is they DID in SQL: x + Null = Null.

Sheesh. That's not a problem, because Python is not trying to be a 
dialect of SQL.

If you want a NULL object, then there are recipes on the web that will 
give you one. Then all you need to do is call sum(alist or [NULL]) and it 
will give you the behaviour you want.


[...]
> Here's a real world example (no ivory tower stuff):
> 
> An oil refinery client has just excavated a big pile of dirt to lay a
> new pipeline. 
[snip details]
> Can't I just use a sum of 0 to tell me when data is missing? No, because
> in some cases the reporting limit of undetected compounds is set to 0.

You can't use a sum of 0 to indicate when data is missing, full stop. The 
data may require 15 tests when only 3 have actually been done:

sum([1.2e-7, 9.34e-6, 2.06e-8])

Missing data and a non-zero sum. How should sum() deal with that?

The answer is that sum() can't deal with that. You can't expect sum() to 
read your mind, know that there should be 15 items instead of 3, and 
raise an error. So why do you expect sum() to read your mind and 
magically know that zero items is an error, especially when for many 
applications it is NOT an error?

The behaviour you want for this specific application is unwanted, 
unnecessary and even undesirable for many other applications. The 
solution is for *you* to write application-specific code to do what your 
application needs, instead of relying on a general purpose function 
magically knowing what you want.



-- 
Steven



More information about the Python-list mailing list