
Vitor Bosshard schrieb:
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())
Or, for sequences: sum(islice(seq, 1), seq[0]) which clearly communicates the need for a non-empty sequence. Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.