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

Andrew Barnert abarnert at yahoo.com
Thu Jul 11 18:54:16 CEST 2013


On Jul 11, 2013, at 7:31, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:

> On 11 July 2013 14:20, Ron Adam <ron3200 at gmail.com> wrote:
>> 
>> On 07/11/2013 06:05 AM, Oscar Benjamin wrote:
>>> On 11 July 2013 11:47, Ron Adam<ron3200 at gmail.com> wrote:
>> 
>>   chain(iters)        # better in most cases...
> 
> I think you mean chain.from_iterable rather than chain
> 
>> I'd like to see chain as a builtin in any case.
> 
> chain.from_iterable should be the builtin not chain.

This is the problem. We can't rename chain.from_iterable to chain (or, equivalently, change the API of chain) while moving it without a lot of confusion. So it seems like we probably need to name it something completely different--concat, flatten, chainiter, ... But none of those names feels right. Concat seems like it should just take two sequences, not a sequence of sequences. Flatten is something you'd only reach for when you're thinking of it as one nested sequence rather than a collection of sequences. Chainiter is ugly. Chain really would be the perfect name if it didn't already have the wrong connotation thanks to its years of life in itertools.

Other than that, I love the idea. The right function for this task should return an iterator, for the same reasons map and zip should, and also because it completely avoids all of the issues with trying to define what it means for different types by only handling iterables and treating them as iterables. The docs for sum already hint that it's the obvious way to concatenate sequences, but it should be more obvious.

If someone can come up with a good name (or just nudge my feeling on one of the already proposed names), I'm definitely +1 on this.


More information about the Python-ideas mailing list