Discussion:
sum(...) limitation
Andrea Griffini
2014-08-01 21:48:37 UTC
Permalink
help(sum) tells clearly that it should be used to sum numbers and not
strings, and with strings actually fails.

However sum([[1,2,3],[4],[],[5,6]], []) concatenates the lists.

Is this to be considered a bug?

Andrea
Guido van Rossum
2014-08-01 21:51:54 UTC
Permalink
No. We just can't put all possible use cases in the docstring. :-)
Post by Andrea Griffini
help(sum) tells clearly that it should be used to sum numbers and not
strings, and with strings actually fails.
However sum([[1,2,3],[4],[],[5,6]], []) concatenates the lists.
Is this to be considered a bug?
Andrea
_______________________________________________
Python-Dev mailing list
https://mail.python.org/mailman/listinfo/python-dev
https://mail.python.org/mailman/options/python-dev/guido%40python.org
--
--Guido van Rossum (python.org/~guido)
Allen Li
2014-08-02 05:57:38 UTC
Permalink
Post by Guido van Rossum
No. We just can't put all possible use cases in the docstring. :-)
help(sum) tells clearly that it should be used to sum numbers and not
strings, and with strings actually fails.
However sum([[1,2,3],[4],[],[5,6]], []) concatenates the lists.
Is this to be considered a bug?
Can you explain the rationale behind this design decision? It seems
terribly inconsistent. Why are only strings explicitly restricted from
being sum()ed? sum() should either ban everything except numbers or
accept everything that implements addition (duck typing).
Terry Reedy
2014-08-02 06:35:32 UTC
Permalink
Loading...