On Sun, Jun 25, 2017 at 3:06 PM, lucas via Python-ideas < python-ideas@python.org> wrote:
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 think a convenient syntax for chaining iterables and sequences would be very useful in Python 3, because there has been a shift from using lists by default to using views to dict keys and values, range objects etc. Having to add an import for a basic operation that used to just work with the + operator feels like a regression to many. It's not really clear if you will be able to implement this, but if you can find a syntax that gets accepted, I think using the same type as itertools.chain might be a good starting point, although the docs should not promise to return that exact type so that support for __getitem__ etc. could be added in the future for cases where the chained iterables are Sequences. -- Koos
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
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- + Koos Zevenhoven + http://twitter.com/k7hoven +