[Python-ideas] Intermediate Summary: Fast sum() for non-numbers

David Mertz mertz at gnosis.cx
Mon Jul 15 18:42:13 CEST 2013


On Mon, Jul 15, 2013 at 12:16 AM, Joshua Landau <joshua at landau.ws> wrote:

> >   flat = chain(list_of_lists)
>
> This does nothing more that iter(list)...
>

Right, I forgot a '*'.  I don't think that changes the point that it's
already very readable, intuitive, and efficient, without resorting to the
counter-intuitive sum().


> >   flat = []
> >   map(flat.extend, list_of_lists)
>
> Gah! No like.


> flat = []
> for lst in list_of_lists:
>     flat.extend(lst)
>

Well,  a couple characters difference, but the explicit loop is fine also.


> You think that "[1, 2, 3] + [4, 5, 6] == [1, 2, 3, 4, 5, 6]" is a
> quirk of python's internals?
>

Basically yes.  Except not quite.  The "quirk" is more that "the plus sign
stands for the .__add__() method, even when it is being used for very
different meanings on different datatypes."  And again, as I point out,
it's not *necessary* that Python had chosen the "+" operator as its
spelling of "concatenation" ... it's a good choice, but it does invite
confusion with the very different use of "+" to mean "addition".

 https://en.wikipedia.org/wiki/Summation

> > Besides numbers, other types of values can be added as well: vectors,
> matrices, polynomials and, in general, elements of any additive group (or
> even monoid).
>

Yep.  And summing every one of those things means *addition* and never
*concatenation*.  There's a reason that article begins with "*Summation* is
the operation of adding <https://en.wikipedia.org/wiki/Addition> a
sequence<https://en.wikipedia.org/wiki/Sequence>of numbers"

It's also notable that concatenation of sequences doesn't form an Abelian
Group.  Hell, concatenation of sequences isn't even commutative.  Using
sum() for a non-commutative operation verges on crazy.  At the least, such
a use is highly counter-intuitive.


> Google's (aggregated) dictionary:
> > The total amount resulting from the addition of two or more numbers,
> amounts, or items
> > the final aggregate; "the sum of all our troubles did not equal the
> misery they suffered" (a good example of where you *already know* you can
> sum things other than numbers*)
>

Summing troubles might resemble addition, metaphorically.  It most
certainly does not resemble concatenation.  *Maybe* somewhere in the
history of English usage you can find some oddball use where the meaning is
vaguely similar to "concatenation."  This is certainly not the common usage
though.


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130715/69c2fcd0/attachment.html>


More information about the Python-ideas mailing list