restriction on sum: intentional bug?

Ethan Furman ethan at stoneleaf.us
Mon Oct 19 01:07:45 CEST 2009


Dave Angel wrote:
> Dieter Maurer wrote:
> 
>> 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
>>
> 
> 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

How is this different than passing a list to sum with other incompatible 
types?

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> class Dummy(object):
...     pass
...
 >>> test1 = [1, 2, 3.4, Dummy()]
 >>> sum(test1)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'float' and 'Dummy'
 >>> test2 = ['a', 'string', 'and', 'a', Dummy()]
 >>> ''.join(test2)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: sequence item 4: expected string, Dummy found

Looks like a TypeError either way, only the verbage changes.

~Ethan~



More information about the Python-list mailing list