sum works in sequences (Python 3)
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Sep 19 12:18:11 EDT 2012
On Wed, 19 Sep 2012 15:07:04 +0000, Alister wrote:
> Summation is a mathematical function that works on numbers Concatenation
> is the process of appending 1 string to another
>
> although they are not related to each other they do share the same
> operator(+) which is the cause of confusion. attempting to duck type
> this function would cause ambiguity for example what would you expect
> from
>
> sum ('a','b',3,4)
>
> 'ab34' or 'ab7' ?
Neither. I would expect sum to do exactly what the + operator does if
given two incompatible arguments: raise an exception.
And in fact, that's exactly what it does.
py> sum ([1, 2, 'a'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
--
Steven
More information about the Python-list
mailing list