Python sum, why not strings? [closed] Python sum, why not strings? [closed] python python

Python sum, why not strings? [closed]


Python tries to discourage you from "summing" strings. You're supposed to join them:

"".join(list_of_strings)

It's a lot faster, and uses much less memory.

A quick benchmark:

$ python -m timeit -s 'import operator; strings = ["a"]*10000' 'r = reduce(operator.add, strings)'100 loops, best of 3: 8.46 msec per loop$ python -m timeit -s 'import operator; strings = ["a"]*10000' 'r = "".join(strings)'1000 loops, best of 3: 296 usec per loop

Edit (to answer OP's edit): As to why strings were apparently "singled out", I believe it's simply a matter of optimizing for a common case, as well as of enforcing best practice: you can join strings much faster with ''.join, so explicitly forbidding strings on sum will point this out to newbies.

BTW, this restriction has been in place "forever", i.e., since the sum was added as a built-in function (rev. 32347)


You can in fact use sum(..) to concatenate strings, if you use the appropriate starting object! Of course, if you go this far you have already understood enough to use "".join(..) anyway..

>>> class ZeroObject(object):...  def __add__(self, other):...   return other...>>> sum(["hi", "there"], ZeroObject())'hithere'


Here's the source: http://svn.python.org/view/python/trunk/Python/bltinmodule.c?revision=81029&view=markup

In the builtin_sum function we have this bit of code:

     /* reject string values for 'start' parameter */        if (PyObject_TypeCheck(result, &PyBaseString_Type)) {            PyErr_SetString(PyExc_TypeError,                "sum() can't sum strings [use ''.join(seq) instead]");            Py_DECREF(iter);            return NULL;        }        Py_INCREF(result);    }

So.. that's your answer.

It's explicitly checked in the code and rejected.