sum(strings)

Steve McAllister nosp at m.needed
Fri Jun 20 06:21:28 EDT 2003


 >>Since strings can be added, they should be sumable too.  No? :-)
 >
 > Nope.

Yes ;-).

 > This caused a real mess when conjoined with such issues as
 > summing empty sequences and summing non-restartable iterators.  The
 > knot was elegantly cut by Guido, who Pronounced that sum wanted to
 > be only and specifically about numbers -- and I think that, as is
 > the case more often than not, he was right.

Sigma(x(i) for i from 0 to n)  ===  x(0) + ... + x(n)

In real life, if (`+' operator)ing right term's operands _is_ 
defined, then the sum _is_ defined.  Inside the Python matrix, 
despite the fact `+' _can_ be used with strings operands, summing 
them is _not_ legal.  This distorsion is precisely what I found 
shoking: `sum' does denote something generic and strongly (only) 
related to the concept of addition.


Anyway, what about using `False' as a neater zero for the default 
start value?, so that:

sum(range(3), False)  # -> 3
sum((), False)        # -> False
sum(('toto', 'tata')) # -> TypeError





More information about the Python-list mailing list