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

Ron Adam ron3200 at gmail.com
Fri Jul 5 18:54:23 CEST 2013



On 07/05/2013 11:38 AM, Steven D'Aprano wrote:
> On 06/07/13 02:22, Ron Adam wrote:
>>
>>
>> On 07/03/2013 07:43 AM, Steven D'Aprano wrote:
>>>
>>> I'm not sure that sum() is the Obvious Way to concatenate lists, and I
>>> don't think that concatenating many lists is a common thing to do.
>>> Traditionally, sum() works only on numbers, and I think we wouldn't be
>>> having this discussion if Python used & for concatenation instead of +. So
>>> I don't care that sum() has quadratic performance on lists (and tuples),
>>> and I must admit that having a simple quadratic algorithm in the built-ins
>>> is sometimes useful for teaching purposes, so I'm -0 on optimizing this
>>> case.
>>
>> I agree, and wished sequences used a __join__ method instead of __add__.
>>
>>
>> The '&' is already used for Bitwise And.  How about '++' instead?
>
> Nope, because it is ambiguous. Given x++y, is that a ++ binary operator, or
> a binary + operator followed by unary + operator?

Yes, I realised that soon after I posted.


> Besides, for backwards compatibility, changing the operator from + cannot
> happen now until Python 4000, if ever.

Right, but if a good solution comes before then,it can be used when the 
time comes.


>>      'hello ' ++ 'world' == "hello world"
>>
>>      [1, 3, 3] ++ [4, 5, 6] == [1, 2, 3, 4, 5, 6]
>>
>>
>> While this doesn't seem like a big change, I think it would simplify code
>> in many places that is more complicated than it really needs to be.
>
>
> I don't think that merely changing an operator from one token + to a very
> slightly different token ++ doesn't "simplify code" in any way. I suggested
> that & was a better token for concatenation because it avoids
> associating/conflating concatenation with addition, but there's no
> difference in code complexity whether you write x+y, x&y or x++y.

It's not the expressions that are improved, but the code surrounding them. 
  Like I said it may not seem like a big change, but I think it would be an 
important change.

Cheers,
    Ron









More information about the Python-ideas mailing list