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

David Eppstein eppstein@ics.uci.edu
Sun, 20 Apr 2003 20:35:24 -0700


In article <20030421025154.GA4542@panix.com>,
 Aahz <aahz@pythoncraft.com> wrote:

> > I agree.  I'd rather see sum() constrain itself to numbers and sum([])
> > == 0.  Then I don't see a need for second argument.  "Summing" a list of
> > strings doesn't make much sense to me.
> 
> Problem is, what *kind* of number?  While ints are in general easily
> promotable (especially int 0), I'd prefer to make things explicit.

Maybe make sum(L) always equivalent to reduce(operator.add,L,0)?
Then "number" here would mean something that can be added to 0, allowing 
any kind of user-defined number type to work (e.g. I recently wanted a 
sum function for Keith Briggs' "xr" package for exact computations over 
computable reals).  This would mean that attempts to abuse sum to 
concatenate strings would raise TypeError.

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science