[Python-Dev] Rationale for sum()'s design?

Eric Nieuwland eric.nieuwland at xs4all.nl
Mon Mar 14 13:34:44 CET 2005


Greg Ewing wrote:
> Guido van Rossum wrote:
>> - the identity (defaulting to 0) if the sequence is empty
>> - the first and only element if the sequence only has one element
>> - (...(((A + B) + C) + D) + ...) if the sequence has more than one 
>> element
>
> While this might be reasonable if the identity
> argument is not specified, I think that if an
> identity is specified, it should be used even
> if the sequence is non-empty. The reason being
> that the user might be relying on that to get
> the semantics he wants.
>
> Think of the second argument as "accumulator
> object" rather than "identity".

+1 for Greg
I think of the second argument as a running total which defaults to the 
operator's neutral element.

Perhaps the second argument should not be optional to emphasise this. 
After all, there's much more to sum() than numbers.

--eric



More information about the Python-Dev mailing list