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

Ron Adam ron3200 at gmail.com
Tue Jul 16 07:50:36 CEST 2013



On 07/15/2013 10:58 PM, Sergey wrote:
> On Jul 15, 2013 Ron Adam wrote:
>
>> >You could copy the code from sum() and make a function with a different
>> >name that specialises in non-number addition.  That would not have any
>> >backwards compatibility issues.

> That would just add one more workaround, but would not solve the
> problem. Sum is already O(N*N) for many containers, and adding
> more workarounds would not make it any faster.

Sorry for not being clearer.  I meant to use your patch for sum as a basis 
to write a new function that is efficient for containers.


> As for me using "+" e.g. to add strings looks rather obvious. Adding
> lists looks similar to adding strings. And sum() looks like a lot of
> "+"es. I mean I see nothing strange in using sum for list of lists.
> It as natural as using max() to find the "largest" word in a list of
> strings — a nice and expected feature. But*if*  in some distant
> python version we would have separate operations for numerical
> addition and sequence concatenation, then we might split our current
> sum() into two functions: sum() and concat().
>
> But to do that we must first solve the problem for sum().
> Or we'll have exactly same problem with concat() anyway.>

Or.. you can solve the problems for concat(), and later when a new major 
version of python is released, we might be able to make sum() use the same 
techniques.  It can work both ways I think.


I got the impression that you already know how, or several possible ways to 
solve the problem with sum(), but are running up against some backward 
compatibility issues?  ie.. can't use __iadd__ in place of __add__.  And 
also some ideological issues about what others think sum() should or 
shouldn't do.  And how much should or should not be special cased.

A new function would allow you to write the function how you think it will 
work the best.  And there would not be any of those issues.


As far as the meaning of the word "sum" goes and how it's used in english, 
It's not the most important issue to me.  Get something to work, and 
demonstrate it works and is useful... then we can have a discussion about 
what to call it.  Take a poll, and if it's still not decided, ask one of 
the core developers to choose from the top name candidates.  That would 
work fine for me.

What's important to me is that I have a way to write nice programs.  Once I 
used whatever function a few times, it's name takes on the meaning of what 
it does.  I just want something I can easily find if I need to look up the 
details for it.

Cheers,
    Ron











More information about the Python-ideas mailing list