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

Raymond Hettinger python@rcn.com
Mon, 21 Apr 2003 16:58:54 -0400


> > So here's a strawman implementation:
> >
> >   def sum(seq, start=0):
> >     if isinstance(start, basestring):
> >       raise TypeError, "can't sum strings; use ''.join(seq) instead"
> >     return reduce(operator.add, seq, start)
> >
> > Alex, go ahead and implement this!
> 
> Coming right up!

For the C implementation, consider bypassing operator.add
and calling the nb_add slot directly.  It's faster and fulfills
the intention to avoid the alternative call to sq_concat.

Also, think about whether you want to match to two argument
styles for min() and max():  
  >>> max(1,2,3)
  3
  >>> max([1,2,3])
  3

When the patch is ready, feel free to assign it to me for
the code review.


Raymond Hettinger


P.S.  Your new builtin works great with itertools.
    def dotproduct(vec1, vec2):
        return sum(itertools.imap(operator.mul, vec1, vec2))