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

Raymond Hettinger python at rcn.com
Sat Dec 5 22:13:45 CET 2009


>>>> > 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?


Use a single univeral zero object that works for everything.
Here's an example from my earlier post:

>>> class Zero:
...     'universal zero for addition'
...     def __add__(self, other):
...         return other
...     def __radd__(self, other):
...         return other
...
>>> Zero() + 'xyz'
'xyz'
>>> sum(['xyz', 'pdq'], Zero())
'xyzpdq'


Raymond



More information about the Python-ideas mailing list