[Python-ideas] Fast sum() for non-numbers - why so much worries?

Steven D'Aprano steve at pearwood.info
Tue Jul 9 18:13:54 CEST 2013


On 09/07/13 19:35, Sergey wrote:
> On Jul 5, 2013 Stefan Behnel wrote:
>
>> No, please. Using sum() on lists is not more than a hack that
>> seems to be a cool idea but isn't. Seriously - what's the sum of
>> lists? Intuitively, it makes no sense at all to say sum(lists).
>
> It's the same as it is now. What else can you think about when you
> see: [1, 2, 3] + [4, 5] ?

Some of us think that using + for concatenation is an abuse of terminology, or at least an unfortunate choice of operator, and are wary of anything that encourages that terminology.

Nevertheless, you are right, in Python 3 both + and sum of lists is well-defined. At the moment sum is defined in terms of __add__. You want to change it to be defined in terms of __iadd__. That is a semantic change that needs to be considered carefully, it is not just an optimization.


> Seriously, why there's so much holy wars about that? I'm not asking
> to rewrite cpython on Java or C#. I'm not adding a bunch of new
> functions, I'm not even changing signatures of existing functions.

Because sometimes people are cautious and conservative about new ideas. Better to be cautious, and miss out on some new function for a version or two, than to rush into it, and then regret it if it turns out to be a bad idea.

I have been very careful to say that I am only a little bit against this idea, -0 not -1. I am uncomfortable about changing the semantics to use __iadd__ instead of __add__, because I expect that this will change the behaviour of sum() for non-builtins. I worry about increased complexity making maintenance harder for no good reason. It's the "for no good reason" that concerns me: you could answer some of my objections if you showed:

- real code written by people who sum() large (more than 100) numbers of lists;

- real code with comments like "this is a work-around for sum() being slow on lists";

- bug reports or other public complaints by people (other than you) complaining that sum(lists) is slow;

or similar. That would prove that people do call sum on lists. But at the moment, I can only judge based on my own experience, both in writing code and dealing with questions on comp.lang.python, and I don't see many people doing sum(lists) or sum(tuples).

Earlier in this discussion, you posted benchmarks for the patched sum using Python 2.7. Would you be willing to do it again for 3.3? And confirm that the Python test suite continues to pass?


Thank you,


-- 
Steven


More information about the Python-ideas mailing list