sum and strings

Paddy paddy3118 at netscape.net
Fri Aug 18 13:30:08 EDT 2006


Sybren Stuvel wrote:
> Paddy enlightened us with:
> > Well, after all the above, there is a question:
> >
> >   Why not make sum work for strings too?
>
> Because of "there should only be one way to do it, and that way should
> be obvious". There are already the str.join and unicode.join methods,
> which are more powerful than sum.
>
> Sybren
> --
> The problem with the world is stupidity. Not saying there should be a
> capital punishment for stupidity, but why don't we just take the
> safety labels off of everything and let the problem solve itself?
>                                              Frank Zappa

Here is where I see the break in the 'flow':

>>> 1+2+3
6
>>> sum([1,2,3], 0)
6
>>> [1] + [2] +[3]
[1, 2, 3]
>>> sum([[1],[2],[3]], [])
[1, 2, 3]
>>> '1' + '2' + '3'
'123'
>>> sum(['1','2','3'], '')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: sum() can't sum strings [use ''.join(seq) instead]
>>> 

- Pad.




More information about the Python-list mailing list