Feature suggestion: sum() ought to use a compensated summation algorithm

Erik Max Francis max at alcyone.com
Sat May 3 17:20:37 EDT 2008


Szabolcs Horvát wrote:

> Arnaud Delobelle wrote:
>>
>> sum() works for any sequence of objects with an __add__ method, not
>> just floats!  Your algorithm is specific to floats.
> 
> This occurred to me also, but then I tried
> 
> sum(['abc', 'efg'], '')
> 
> and it did not work.  Or is this just a special exception to prevent the 
>  misuse of sum to join strings?  (As I said, I'm only an occasional user.)

What you wrote is nonsensical there, no different from 'a' + 1 -- which 
is why it quite rightly raises a TypeError.

You're trying to add a list to a string, which is nonsensical.  You add 
strings to strings, or lists to lists, but mixing them up doesn't make 
sense.  Python can't guess what you mean when you write something like 
['abc', 'def'] + '' -- which is the functional equivalent of your call 
to sum -- and so doesn't try.  It indicates this by raising a TypeError.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis



More information about the Python-list mailing list