[Python-Dev] sum(...) limitation

Devin Jeanpierre jeanpierreda at gmail.com
Sat Aug 9 23:07:58 CEST 2014

On Sat, Aug 9, 2014 at 12:20 PM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Sat, Aug 9, 2014 at 1:08 AM, Steven D'Aprano <steve at pearwood.info> wrote:
>> We wouldn't be having
>> these interminable arguments about using sum() to concatenate strings
>> (and lists, and tuples) if the & operator was used for concatenation and
>> + was only used for numeric addition.
> But we would probably have a similar discussion about all(). :-)
> Use of + is consistent with the use of * for repetition.  What would you use
> use for repetition if you use & instead?

If the only goal is to not be tempted to use sum() for string
concatenation, how about using *? This is more consistent with
mathematics terminology, where a * b is not necessarily the same as b
* a (unlike +, which is commutative). As an example, consider matrix
multiplication. Then, to answer your question, repetition would have
been s ** n.

(In fact, this is the notation for concatenation and repetition used
in formal language theory.)

(If we really super wanted to add this to Python, obviously we'd use
the @ and @@ operators. But it's a bit late for that.)

-- Devin

More information about the Python-Dev mailing list