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

Ron Adam ron3200 at gmail.com
Sat Jul 6 11:02:37 CEST 2013



On 07/05/2013 08:53 PM, Stephen J. Turnbull wrote:
> Terry Reedy writes:
>
>   > In my experience of American English, normal people 'add' lists
>
> Nitpick: That somehow doesn't sit right with me.  They "put lists
> together" or "include one in another" (assign to slice, not list as
> element of superior list).  Adding refers to elements: "add item to
> list", although it's often applied iteratively (being synonymous to
> "include in")..

We combine lists, and add ingredients.  Or do we combine ingredients and 
add lists?  ;-)


> I don't think it's appropriate to refer to any specific English usage
> here.

It's one of those things that if we can find a relationship that works, 
great, but it just won't always work out that well.

> Most native speakers, and especially programmers, have
> flexibility in the direction of allowing the generalization of "+" to
> strings to imply that "sum" also generalizes to strings, as the
> iterated and associative application of "+".  They're even more
> flexible than that: they intuitively understand that although "+" (and
> perhaps even more so) usually imply "commutative" (as in the
> convention in abstract algebra), when applied to strings and other
> sequences this doesn't make sense (as of course it doesn't make sense
> for infinite sequences in analysis).
>
> I think that as far as consistency with intuition goes, it doesn't
> matter which we choose: we really need to ask the ultimate authority
> on "Pythonicity".  My own preference is based on "consenting adults":
> if "sum(list_of_strings)" bothers you, don't use it.  But I don't
> understand the ramifications of thoroughly implementing that in a
> community where intuition splits as deeply as it evidently does here.

It helps to view these thing from two sides.  On the human side, we want 
something that is somewhat familiar, but on the CPU side, we need to be 
fairly specific.   Humans tend to overgeneralise, and over simplify, but we 
also are able to infer more specific information by including lots of 
extraneous information.   Python is very limited in that respect.

On one hand it sometimes is nice to be able to write very general routines 
that don't care what the input is, or will work with a wide range of input. 
  But if the routine is too generalized, it can do the wrong thing at 
times.  Like iterating letters in a string in a list.

So having the language organised in meaningful ways can help.  The question 
is, is there something we can change that will help with things like this?

The behaviour I would like is...  (only some examples.)

      add and subtract scaler objects  (and other scaler operations.)

      combine and separate immutable collections  (strings, tuples)

      Join and split mutable collections  (lists, dictionaries, sets)


We already get some nice error messages when try to do things that don't 
make sense, but sometimes, we get a wrong result instead.

Cheers,
    Ron
















More information about the Python-ideas mailing list