restriction on sum: intentional bug?

Luis Zarrabeitia kyrie at
Tue Oct 27 16:20:47 CET 2009

On Tuesday 27 October 2009 07:29:46 am Steve wrote:
> To me the current implementation is blatantly correct. sum is an
> operation on a list of numeric values, returning a numeric value -
> this is why we have the seemingly strange "".join() rather than [].join
> ("")

But then, why does sum() accept non-numeric input? How does sum decides if the 
arguments are "numbers", given that it accepts almost everything that defines 
__add__ (except strings)?

I really never tried to sum() strings before, but it makes sense that, if 
sum() already checks if the argument is a string before, it should do 
the "".join instead of returning an error.

Also, why is there a special case for the strings, but not for the tuples? 
Doesn't sum(((1,) for _ in xrange(100)),()) also have quadratic behaviour, 
creating and destroying intermediate tuples? And what happens with lists? 

Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.

More information about the Python-list mailing list