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

Steven D'Aprano steve at pearwood.info
Fri Jul 5 18:38:40 CEST 2013


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?

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



>      '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.



-- 
Steven


More information about the Python-ideas mailing list