[Python-ideas] Why does `sum` use a default for the `start` parameter?

Vitor Bosshard algorias at gmail.com
Sat Dec 5 19:55:53 CET 2009

2009/12/5 Ram Rachum <cool-rr at cool-rr.com>:
> MRAB <python at ...> writes:
>> > I prefer (b). The problem with requiring `start` for sequences of non-
> numerical
>> > objects is that you now have to go out and create a "zero object" of the
> same
>> > type as your other objects. The object class might not even have a concept
> of a
>> > "zero object".
>> >
>> If the objects can be summed, shouldn't there also be a zero object?
>> Does anyone have an example when that's not possible?
> You're right MRAB, probably almost every object type that has a concept of
> "addition" will have a concept of a zero element.
> BUT, that zero object has to be created by the user of `sum`, and that has two
> problems:
> 1. The user might not know from beforehand which type of object he's adding.
> Even within the same type there might be problems. What happens when the user is
> using `sum` to add a bunch of vectors, and he doesn't know from beforehand what
> the dimensions of the vectors are? How will he know if his zero element should
> be Vector([0, 0]) or Vector([0, 0, 0])

Ugly, but works:

itr = iter(sequence)
sum(itr, itr.next())

This is actually a good example in favor of not requiring a start value.

More information about the Python-ideas mailing list