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

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Mar 17 02:31:43 CET 2005

Tim Peters wrote:

> I can't say it bothers me to specify an appropriate identity element
> when 0 is inappropriate.

Maybe instead of a single sum() function, each summable
type should have a sum() static method which uses an
identity appropriate to that type.

So to sum a list of integers you would use int.sum(x),
to sum floats you would use float.sum(x), and to sum
timedeltas you would use timedelta.sum(x).

This would also neatly solve the problem of people
trying to sum strings, because there would be no
str.sum() method.

Or alternatively, there could be a str.sum(x) which
was equivalent to "".join(x). Although it might be
better to call it str.concat() instead of str.sum().

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+

More information about the Python-Dev mailing list