[Python-ideas] Fast sum() for non-numbers
Chris Kaynor
ckaynor at zindagigames.com
Tue Jul 9 18:42:55 CEST 2013
On Tue, Jul 9, 2013 at 7:07 AM, Ronald Oussoren <ronaldoussoren at mac.com>wrote:
> The better solution is to not use sum. I haven't looked at your patch,
> but does it deal with all edge cases (such as calling sum on a heterogenous
> list that happens to have a tuple as its first item)? Trying to
> special-case
> sum for a sequence of tuples is (IMHO too) magical, and getting all details
> right makes the code more complicated.
>
> Sum is primarily intented for summing sequences of numeric values, that
> it works on list and tuples as well is a more or less unintended
> side-effect,
> and btw. the documentation for sum explicitly says its mentioned to sum
> numbers:
>
> sum(iterable[, start]) -> value
>
> Returns the sum of an iterable of numbers (NOT strings) plus the value
> of parameter 'start' (which defaults to 0). When the iterable is
> empty, returns start.
I wonder if the best solution, if sum is only intended for use on numbers,
would be to move it to the math module, rather than being a built-in
function.
No other changes would need to be made, although the special case for
strings could likely be removed then as it would become fairly obvious that
the string case is not reasonably supported.
I would imagine that this would not take place until Python 4 (due to the
large amount of existing code it would break), and I am not really
proposing it, but it would seem to be logical.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130709/323d246c/attachment.html>
More information about the Python-ideas
mailing list