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

Ivan Illarionov ivan.illarionov at gmail.com
Sat May 3 17:37:25 EDT 2008


On Sat, 03 May 2008 20:44:19 +0200, 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'], '')

Interesting, I always thought that sum is like shortcut of
reduce(operator.add, ...), but I was mistaken.

reduce() is more forgiving:
reduce(operator.add, ['abc', 'efg'], '' ) # it works
'abcefg'

-- 
Ivan



More information about the Python-list mailing list