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

Ram Rachum cool-rr at cool-rr.com
Sat Dec 5 19:42:31 CET 2009

MRAB <python at ...> writes:

> > I prefer (b). The problem with requiring `start` for sequences of non-
> > objects is that you now have to go out and create a "zero object" of the 
> > 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 

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])

2. A smaller problem: The user has to actually create that zero object now, and 
for some objects the definition might be lengthy, adding needless complexity to 
the code.

Also, using the `start` has some overhead, for creating the zero object and 
calling __add__.


More information about the Python-ideas mailing list