restriction on sum: intentional bug?

Dieter Maurer dieter at handshake.de
Sun Oct 18 01:47:01 EDT 2009


Christian Heimes <lists at 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




More information about the Python-list mailing list