I would like to add that for example numpy ndarrays are iterables, but they have an __add__ with completely different semantics, namely element-wise ( numerical) addition.
So this proposal would conflict with existing libraries with iterable objects. Stephan Op 25 jun. 2017 2:51 p.m. schreef "Serhiy Storchaka" <storch...@gmail.com>: > 25.06.17 15:06, lucas via Python-ideas пише: > >> 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)`) >> > > It would be weird if the addition is only supported for instances of the > generator class, but not for other iterators. Why (n for n in range(2)) + > (n for n in range(2, 4)) works, but iter(range(2)) + iter(range(2, 4)) and > iter([0, 1]) + iter((2, 3)) don't? itertools.chain() supports arbitrary > iterators. Therefore you will need to implement the __add__ method for > *all* iterators in the world. > > However itertools.chain() accepts not just *iterators*. It works with > *iterables*. Therefore you will need to implement the __add__ method also > for all iterables in the world. But __add__ already is implemented for list > and tuple, and many other sequences, and your definition conflicts with > this. > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/