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

Prabhu Ramachandran prabhu@aero.iitm.ernet.in
Sun, 20 Apr 2003 11:39:23 +0530

>>>>> "AM" == Alex Martelli <aleax@aleax.it> writes:

[summing a sequence]
    AM> Now, I think the obvious approach would be to have a function
    AM> sum, callable with any non-empty homogeneous sequence
    AM> (sequence of items such that + can apply between them),
    AM> returning the sequence's summation -- now THAT might help for
    AM> simplicity, clarity AND power.

FWIW, Numeric provides a sum function that mostly does what you want:

>>> from Numeric import * 
>>> sum(range(999))
>>> sum(['a', 'b', 'c'])

# this one produces a slightly surprising result
>>> sum(['aaa', 'b', 'c'])
array([abc , a   , a   ],'O')
# but is easily explained in the context of multi-dimensional arrays.

Anyway, my point is most Numeric users are already comfortable with
the idea of a sum function.  However, as someone already said, if you
argue that sum is necessary, what about product (which again Numeric
provides along with a host of other useful functions)?