[Python-ideas] Use the plus operator to concatenate iterators

Grayson, Samuel Andrew sag150430 at utdallas.edu
Wed Aug 5 03:21:45 CEST 2015


In the case that `__add__` is already defined by the iterator, I propose to use that. Otherwise, I propose to concatenate the iterators.

This serves two purposes: convenience and backwards-compatibility/consistency.

Convenience: Imagine having to do `operator.mul(5, 4)` instead of `5 * 4`, or `list_chain([1, 2, 3], [4, 5, 6])` instead of `[1, 2, 3] + [4, 5, 6]`. Following this pattern for commonly used operators, `range(5) + 'abc'` instead of `itertools.chain(range(5), 'abc'`.

(notice also that if you act like a list, but redefine the __add__ method, then the default behavior is overridden. In the same way, I propose that if you redefine the __add__ method, then the proposed default behavior (concatenanation) is overridden.

Backwards-compatibility: This helps backwards-compatibility where lists in python2 changed to iterators in python3, everywhere except for concatenation via the plus operator.


More information about the Python-ideas mailing list