Vitor Bosshard schrieb:
2009/12/5 Ram Rachum email@example.com:
MRAB <python@...> 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
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:
- 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())
Or, for sequences:
sum(islice(seq, 1), seq)
which clearly communicates the need for a non-empty sequence.