[Python-ideas] Fast sum() for non-numbers

Ronald Oussoren ronaldoussoren at mac.com
Tue Jul 9 16:07:57 CEST 2013


On 9 Jul, 2013, at 15:42, Sergey <sergemp at mail.ru> wrote:

> On Jul 8, 2013 Andrew Barnert wrote:
> 
>> I'm -1 on adding special-casing for tuples that would not be
>> available for any other immutable type. 
> 
> Ok, let's be honest, I don't like that special case too. :(
> But when I had two options:
> 
> 1. Make sum faster for everything BUT tuples and write in a manual:
>    ...
>    sum() is fast for all built-in types except `tuple`. For tuples
>    you have to manually convert it to list, i.e. instead of:
>        sum(list_of_tuples, tuple())
>    you have to write:
>        tuple(sum(map(list,list_of_tuples),[]))
>    or
>        tuple(itertools.chain.from_iterable(list_of_tuples))
>    ...
> 
> 2. Implement just one (!) special case for the only type in python
>   needing it and write:
>    ...
>    sum() is fast for all built-in types!
>    ...
> 
> I chose #2. Tuple is one of the most frequently used types in python,
> and it's the only type that needs such a special case.
> 
> Until someone writes a better solution: Practicality beats purity.
> That was my motivation.

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.


Ronald



More information about the Python-ideas mailing list