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

Guido van Rossum gvanrossum at gmail.com
Mon Mar 14 03:39:33 CET 2005


[Guido van Rossum]
> >
> > - 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

[Greg Ewing]
> 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".

But I think the logical consequence of your approach would be that
sum([]) should raise an exception rather than return 0, which would be
backwards incompatible. Because if the identity element has a default
value, the default value should be used exactly as if it were
specified explicitly.

Unfortunately my proposal is also backwards incompatible, since
currently sum([1,1], 40) equals 42.

I guess nobody remembers why we did it the way it is?

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list