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

Alex Martelli aleaxit at yahoo.com
Mon Mar 14 08:31:23 CET 2005


On Mar 14, 2005, at 01:22, 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 to Greg's idea -- I do have cases where the items arrive in 
irregular bunches and I maintain the running total via this mechanism, 
initializing as

running_total = 0

and updating it as

running_total = sum(bunch_of_items, running_total)


Back to Guido's request for the history of how the design evolved, I 
did some googling -- it all happened on this mailing list, April 19 to 
April 21, 2003.  Most of it subject:  Fwd: summing a bunch of numbers 
(or "whatevers"), though some had Re: and other lacked Fwd: 
decorations, in case you're searching on the month's archives by 
subject.

Reading the whole thread will help with the pro's and con's, but the 
conclusions are mostly in Guido's post 
http://mail.python.org/pipermail/python-dev/2003-April/034853.html and 
my concurring reply 
http://mail.python.org/pipermail/python-dev/2003-April/034855.html .


Alex



More information about the Python-Dev mailing list