[Python-ideas] + operator on generators

Cameron Simpson cs at zip.com.au
Fri Jun 30 19:39:12 EDT 2017


On 28Jun2017 09:54, Paul Moore <p.f.moore at gmail.com> wrote:
>On 28 June 2017 at 05:30, Terry Reedy <tjreedy at udel.edu> wrote:
>> On 6/27/2017 10:47 PM, Nick Coghlan wrote:
>>
>>> While I haven't been following this thread closely, I'd like to note
>>> that arguing for a "chain()" builtin has the virtue that would just be
>>> arguing for the promotion of the existing itertools.chain function
>>> into the builtin namespace.
>>>
>>> Such an approach has a lot to recommend it:
>>>
>>> 1. It has precedent, in that Python 3's map(), filter(), and zip(),
>>> are essentially Python 2's itertools.imap(), ifilter(), and izip()
>>> 2. There's no need for a naming or semantics debate, as we'd just be
>>> promoting an established standard library API into the builtin
>>> namespace
>>
>>
>> A counter-argument is that there are other itertools that deserve promotion,
>> by usage, even more.  But we need to see comparisons from more that one
>> limited corpus.
>
>Indeed. I don't recall *ever* using itertools.chain myself. I'd be
>interested in seeing some usage stats to support this proposal. As an
>example, I see 8 uses of itertools.chain in pip and its various
>vendored packages, as opposed to around 30 uses of map (plus however
>many list comprehensions are used in place of maps). On a very brief
>scan, it looks like the various other itertools are used less than
>chain, but with only 8 uses of chain, it's not really possible to read
>anything more into the relative frequencies.

I don't use it often, but when I do it is very handy. While I'm not arguing for 
making it a builtin on the basis of my own use (though I've no objections 
either), a quick grep shows:

My maildb kit uses chain to assemble multiple related header values:

    *chain( msg.get_all(hdr, [])
            for hdr
            in ('to', 'cc', 'bcc', 'resent-to', 'resent-cc') )

Two examples where I use it to insert items in front of an iterable:

    chunks = chain( [data], chunks )

    blocks = indirect_blocks(chain( ( topblock, nexttopblock ), blocks ))

Neither of these is amenable to list rephrasings because the tail iterables 
("chunks" and "blocks") are of unknown and potentially large size.

And a few other cases whose uses are harder to succinctly describe, but 
generally "iterable flattening".

So it is uncommon for me, but very useful when I want it.

Just some (small) data points.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Python-ideas mailing list