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

Brett Cannon drifty@alum.berkeley.edu
Sat, 19 Apr 2003 17:01:21 -0700 (PDT)

[Alex Martelli]

> For the Nth time, today somebody asked in c.l.py about how best to sum
> a list of numbers.  As usual, many suggested reduce(lambda x,y:x+y, L),
> others reduce(int.__add__,L), others reduce(operator.add,L), etc, and some
> (me included) a simple
>     total = 0
>     for x in L:
>         total = total + x
> Now, I think the obvious approach would be to have a function sum,
> callable with any non-empty homogeneous sequence (sequence of
> items such that + can apply between them), returning the sequence's
> summation -- now THAT might help for simplicity, clarity AND power.
> Discussing this with newbie-to-moderately experienced Pythonistas,
> the uniform reaction was on the order of "you mean Python doesn't
> HAVE a sum function already?!" -- most everybody seemed to feel
> that such a function WOULD be "the obvious way to do it" and that
> it should definitely be there.

So I have no fundamental issue with the proposed function, but I don't
find a huge need for it personally; I always do the looping solution
(jaded against the functional stuff from school =).

I do see how it could be useful, though.  I don't necessarily see this as
a built-in (although it wouldn't kill me if it became one).  I don't see
it going into either the math or operator modules since it doesn't quite
fit what is already there.  I initially thought itertools since it is
basically working on an iterator, but I don't know if we want to change
itertools from a module the provides functionality for outputting special
iterators compared to working with iterators.

And as for the argument that other people are shocked it isn't already
there... I just don't agree with that.  Just because people want it does
not mean it is a good solution to a problem.  Tyranny of the majority and
such.  =)

So I am currently +0 on having the function, -0 on sticking it in math or
operator, +0 on built-in.

And now I go back to PHP grunt work, wishing I was actually writing docs
for test_support and regrtest instead (and that says something about what
I am having to work on).