[Python-ideas] + operator on generators

lucas lucas.bourneuf at laposte.net
Sun Jun 25 08:06:54 EDT 2017


Hello !

I often use generators, and itertools.chain on them.
What about providing something like the following:

    a = (n for n in range(2))
    b = (n for n in range(2, 4))
    tuple(a + b)  # -> 0 1 2 3

This, from user point of view, is just as how the
__add__ operator works on lists and tuples.
Making generators works the same way could be a great way to avoid calls
to itertools.chain everywhere, and to limits the differences between
generators and other "linear" collections.

I do not know exactly how to implement that (i'm not that good at C, nor
CPython source itself), but by seeing the sources,
i imagine that i could do something like the list_concat function at
Objects/listobject.c:473, but in the Objects/genobject.c file,
where instead of copying elements i'm creating and initializing a new
chainobject as described at Modules/itertoolsmodule.c:1792.

(In pure python, the implementation would be something like `def
__add__(self, othr): return itertools.chain(self, othr)`)

Best regards,
--lucas

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170625/901379b0/attachment-0001.sig>


More information about the Python-ideas mailing list