Dieter Maurer wrote:
Christian Heimes <li...@cheimes.de> writes on Fri, 16 Oct 2009 17:58:29 +0200:
Alan G Isaac schrieb:
I expected this to be fixed in Python 3:

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

Of course it is not a good way to join strings,
but it should work, should it not?  Naturally,
It's not a bug. sum() doesn't work on strings deliberately. ''.join()
*is* the right and good way to concatenate strings.

Apparently, "sum" special cases 'str' in order to teach people to use "join".
It would have been as much work and much more friendly, to just use "join"
internally to implement "sum" when this is possible.

Dieter



Earlier, I would have agreed with you. I assumed that this could be done invisibly, with the only difference being performance. But you can't know whether join will do the trick without error till you know that all the items are strings or Unicode strings. And you can't check that without going through the entire iterator. At that point it's too late to change your mind, as you can't back up an iterator. So the user who supplies a list with mixed strings and other stuff will get an unexpected error, one that join generates.

To put it simply, I'd say that sum() should not dispatch to join() unless it could be sure that no errors might result.

DaveA

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to