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

Joshua Landau joshua at landau.ws
Mon Jul 15 19:54:46 CEST 2013


On 15 July 2013 17:42, David Mertz <mertz at gnosis.cx> wrote:
> 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

I agree in that chain.from_iterable is currently TOOWTDI and I don't
think need that to change.

> , without resorting to the
> counter-intuitive sum().
>
>> 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".

But it *is* addition as far as Python is concerned.

I don't care much about that Python's "+" aren't exclusively for any
particular groups; heck -- I'd be happy if we could add a ton *more*
things than we currently can. It's still addition; we're not planning
Haskell here.

*Even if* it was a good idea to restrict "+" to commutative groups
with constant-time addition (which it is not), the ship has sailed and
addition in Python means what it does. [Hence sum, being
“reduce(.__add__, iterable)” so to speak, makes a ton of sense on
lists.]

>>  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 a sequence of numbers"

As far as Python is concerned, contancation is a form of addition.
Maybe not in mathematics, nor Haskell, nor C. But it is in Python, so
lists in Python are additive groups.

> It's also notable that concatenation of sequences doesn't form an Abelian
> Group.  Hell, concatenation of sequences isn't even commutative.

Notable? Yeah, fine. Important? No.

> Using
> sum() for a non-commutative operation verges on crazy.  At the least, such a
> use is highly counter-intuitive.

Why? It's not to me. Sure, you need to know the order that it will
operate, but you need to know that for "reduce" too and no-one says
using "reduce" in non-commutative ways is insane.

>> 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.

Yet another way in which we disagree.

> *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.

Oh, again we disagree.


More information about the Python-ideas mailing list