PEP 322: Reverse Iteration (REVISED, please comment)

Michele Simionato mis6 at pitt.edu
Thu Nov 6 01:59:56 EST 2003


Alex Martelli <aleax at aleax.it> wrote in message news:<EBVnb.376111$R32.12466927 at news2.tin.it>...
> math.sum would be arguably equivalent to sum as a built-in -- a
> tad less immediately accessible, but perhaps superior in that it
> immediately suggests it's about numbers only, so we'd avoid the
> icky performance trap you now get with sum(manylists, []) {which
> makes sum only 2/3 wonderful at best -- fine with numbers ONLY}.

A recent post of yours made me realize that "sum" is NOT for
numbers only:

>>> class C(object):
	def __add__(self,other):
		return self

>>> c1,c2,c3=C(),C(),C()
>>> sum([c1,c2],c3)
<__main__.C object at 0x00F4DED0>

So, only summing strings is disallowed, all the rest is allowed, but
there can be performance traps, for instance in summing lists. So, if
'sum' is only good at summing numbers, why this is not enforced?
We are forbidden to sum strings, but then for the same reason we
should be forbidden to sum lists: there is some inconsistency here ...
care to explain the rationale for 'sum' again ?


                Michele




More information about the Python-list mailing list