[docs] [issue23787] sum() function docstring lists arguments incorrectly

Wolfgang Maier report at bugs.python.org
Sat Mar 28 23:58:28 CET 2015


Wolfgang Maier added the comment:

>This implies sum() should accept str, unicode, list, tuple, bytearray, buffer, and xrange.

and in fact it *does* accept all these as input. It just refuses to add the elements of the sequence if these elements are of certain types. Of course, the elements of a string are strings themselves so this does not work:

>>> sum('abc', '')
Traceback (most recent call last):
  File "<pyshell#88>", line 1, in <module>
    sum('abc', '')
TypeError: sum() can't sum strings [use ''.join(seq) instead]


compare with a bytes sequence in Python3, where the elements are ints:

>>> sum(b'abc', 0)
294


but strings are also perfectly accepatble as input if you do not try to add their str elements, but something else:

>>> class X (int):
    def __add__(self, other):
        return X(ord(other) + self)

>>> sum('abc', X(0))
294

=> the docs are right and there is no issue here.

----------
nosy: +wolma

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23787>
_______________________________________


More information about the docs mailing list