2009/12/5 Ram Rachum <cool-rr@cool-rr.com>:
MRAB <python@...> 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.