[Python-ideas] Why does `sum` use a default for the `start` parameter?
cool-rr at cool-rr.com
Sat Dec 5 17:56:09 CET 2009
> Sometimes you might find that the list you're summing is empty. Because
> 'sum' is most often used with numbers, the default sum of a list is 0.
> If you want to sum a list of non-numbers, provide a suitable start
> value. For example, to sum a list of lists a suitable start value is :
> >>> sum([[0, 1], [2, 3]], )
> [0, 1, 2, 3]
> I agree that it would be nice if the start value could just be omitted,
> but then what should 'sum' return if the list is empty?
I see the problem. I think a good solution would be to tell the user, "If you
want `sum` to be able to handle a non-empty list, you must supply `start`."
Users that want to add up a (possibly empty) sequence of numbers will have to
If start is supplied, it will work like it does now. If start isn't supplied, it
will add up all the elements without adding any `start` to them.
What do you think?
More information about the Python-ideas