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

Guido van Rossum gvanrossum at gmail.com
Sun Mar 13 17:38:42 CET 2005

There are a few design choices we could have made for sum(); in
particular, for non-empty sequences we could not have used the
identity element (the optional second argument). As it is, we get
unjustified but understandable complaints that sum() "only supports
numbers". An alternative design could have returned:

- 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

This has surprises too (in particular of returning 0 when invoked
without overriding the identity argument for a seqence of addable
non-numbers) but works without using the second argument for a larger
set of inputs I believe it is often used in such a way that the input
is known to be non-empty).

I'd be happy to be pointed to a past discussion where this was
considered and rejected with a good reason; then I can post that to
the blog (where the deficiency in sum() is being berated a bit

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

More information about the Python-Dev mailing list