On Wed, Aug 5, 2015 at 10:22 AM, Grayson, Samuel Andrew <sag150430@utdallas.edu> wrote:
I propose:
iter([1, 2, 3]) + iter([4, 5, 6]) # evaluates to something like itertools.chain(iter([1, 2, 3]), iter([4, 5, 6])) # equivalent to iter([1, 2, 3, 4, 5, 6])
Try this: class iter: iter = iter # snapshot the original iter() def __init__(self, iterable): self.it = self.iter(iterable) self.next = [] def __iter__(self): return self def __next__(self): while self.next: try: return next(self.it) except StopIteration: self.it, *self.next = self.next return next(self.it) # Allow StopIteration to bubble when it's the last one def __add__(self, other): result = self.__class__(self.it) result.next = self.next + [self.iter(other)] return result As long as you explicitly call iter() on something, you get the ability to add two iterators together. I haven't checked for odd edge cases, but something like this does work, and should work on all Python versions. ChrisA