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

Duncan Booth duncan.booth at invalid.invalid
Mon May 5 06:24:00 EDT 2008


Szabolcs <szhorvat at gmail.com> wrote:

> On May 5, 9:37 am, Szabolcs Horvát <szhor... at gmail.com> wrote:
>> Gabriel Genellina wrote:
>>
>> > Python doesn't require __add__ to be associative, so this should
>> > not be 
> used as a general sum replacement.
>>
>> It does not _require_ this, but using an __add__ that is not
>> commutative and associative, or has side effects, would qualify as a
>> serious misuse, anyway.
> 
> Sorry, I meant associative only, not commutative.
> 
Unfortunately an __add__ which is not associative is actually perfectly 
reasonable. For example you could have a Tree class where adding two trees 
returns a new tree so (a+b)+c gives you:

    .
   / \
   .  c
  / \
  a  b

but a+(b+c) gives:

    .
   / \
   a  .
     / \
     b  c
   



More information about the Python-list mailing list