[Python-ideas] + operator on generators

Nick Coghlan ncoghlan at gmail.com
Wed Jun 28 05:09:41 EDT 2017


On 28 June 2017 at 18:54, Paul Moore <p.f.moore at gmail.com> wrote:
> On 28 June 2017 at 05:30, Terry Reedy <tjreedy at udel.edu> wrote:
>> 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.

The other thing to look for would be list() and list.extend() calls. I
know I use those quite a bit in combination with str.join, where I
don't actually *need* a list, it's just currently the most convenient
way to accumulate all the components I'm planning to combine. And if
you're converting from Python 2 code, then adding a few list() calls
in critical places in order to keep the obj.extend() calls working is
likely to be easier in many cases than switching over to using
itertools.chain.

For simple cases, that's fine (since a list of direct references will
be lower overhead than accumulating a chain of short iterables), but
without builtin support for iterator concatenation, it's currently a
nonlocal refactoring (to add the "from itertools import chain" at the
top of the file) to switch completely to the "pervasive iterators"
model when folks actually do want to do that.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list