[Python-ideas] Fast sum() for non-numbers

Ron Adam ron3200 at gmail.com
Thu Jul 11 15:20:53 CEST 2013



On 07/11/2013 06:05 AM, Oscar Benjamin wrote:
> On 11 July 2013 11:47, Ron Adam<ron3200 at gmail.com>  wrote:
>> >
>> >I would like sum to (eventually) be moved to the math module and have it's
>> >API and behaviour be the same as fsum.  That would have the least surprises
>> >and it reduces the mental load when two similar functions act the same and
>> >can be found near each other in the library.
> Sum is*really*  useful. You can use it for ints, for floats, for numpy
> arrays, for Decimals, for Fractions, for sympy.Expressions, for
> gmpy2...
>
> math.fsum is only useful for scalar floats but sum does so much more.
> There's no need to cripple it just just to stop people from summing
> lists.

Isn't it just a matter of spelling it a bit different?


    sum(iters, [])      # how often do you actually use this?

vs...

    chain(iters)        # better in most cases...

    list(chain(iters))  # when you actually need a combined list.



I'd like to see chain as a builtin in any case.

Or to look at it in another way... I wouldn't want to add the ability to 
sum items in a list to chain().


Note, because to get sum() to join lists requires it to be explicitly 
spelled with a starting list, it doesn't need to be changed.  I'm +1 for 
doing that, only if there is a consensus for doing that.

How do you feel about adding the ability of sum to sum vectors or lists of 
values to each other?

sum([[x1, y1], [x2, y2], ...])  --->  [x1+x2, y1+y2]


Cheers,
    Ron






More information about the Python-ideas mailing list