[Python-Dev] Fwd: summing a bunch of numbers (or "whatevers")

Alex Martelli aleax@aleax.it
Sun, 20 Apr 2003 08:29:52 +0200


On Sunday 20 April 2003 08:09 am, Prabhu Ramachandran wrote:
   ...
> Anyway, my point is most Numeric users are already comfortable with
> the idea of a sum function.  However, as someone already said, if you

Oh yes, Numeric.sum is excellent, by all means.  But I think sum is
quite helpful even for programs not using Numeric.

> argue that sum is necessary, what about product (which again Numeric
> provides along with a host of other useful functions)?

In the context of Numeric use, it's quite appropriate to have sum, prod,
and the other ufuncs' reduce AND accumulate methods.  In everyday
programming in other fields, the demand for the functionality given by
sum is FAR higher than that given by prod.  For example, googling on
c.l.py shows 165 posts mentioning "reduce(operator.add" versus 39
mentioning "reduce(operator.mul".  This reflects the need of typical
computations -- indeed, even the English language shows indications
about the prevalence of summing as a bulk operation.  

In everyday life, we often have to sum a set of numbers of varying
cardinality -- we even have the word "total" to indicate the result of
this operation.  We rarely have to multiply such a set of numbers --
most multiplications we do involve two, at most three numbers, while
every time we check a restaurant bill or other itemized bill we're
summing up a varying number of numbers, for example.

I think that, in this case, practicality beats purity, and we should have
a sum function somewhere in Python's standard library (or builtins,
though as someone mentioned they ARE quite fat already), leaving
reduce for all other, less frequently used cases of bulk operations.


Alex