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

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


If we are barking up that tree,

    import itertools

    _old_iter = iter
    class iter (object):
        def __init__(self, it):
            self.it = _old_iter(it)
        def __iter__(self):
            return self
        def __next__(self):
            return next(self.it)
        def __add__(self, other):
            return iter(itertools.chain(self.it, other))
        def __radd__(self, other):
            return iter(itertools.chain(other, self.it))

    >>> list('wxy' + iter([1, 2]) + range(3, 5) + 'abc')
    ['w', 'x', 'y', 1, 2, 3, 4, 'a', 'b', 'c']



More information about the Python-ideas mailing list