[Python-Dev] Iterator addition?

Moshe Zadka m@moshez.org
Mon, 30 Jul 2001 11:38:45 +0300

On Mon, 30 Jul 2001, Sami Hangaslammi <shang@cc.jyu.fi> wrote:

> Since iterator objects work like sequences in several contexts, maybe they
> could support sequence-like operations such as addition. This would let
> you write
>   for x in iter1 + iter2:
>       do_something(x)
> instead of
>   for x in iter1:
>       do_something(x)
>   for x in iter2:
>       do_something(x)
> or the slightly better
>   for i in iter1,iter2:
>       for x in i:
>           do_something(x)

No, instead of:

class concat:

    def __init__(self, *iterators):
        self.iterators = list(iterators)

    def __iter__(self): return self

    def next(self):
        while self.iterators:
                return self.iterators[0].next() 
            except StopIteration:
                del self.iterators[0]
            raise StopIteration

for x in concat(iter1, iter2):

(Note that the first n-2 lines can be refactored. Wasn't there talk
about having an iterator module with useful stuff like that?)

gpg --keyserver keyserver.pgp.com --recv-keys 46D01BD6 54C4E1FE
Secure (inaccessible): 4BD1 7705 EEC0 260A 7F21  4817 C7FC A636 46D0 1BD6
Insecure (accessible): C5A5 A8FA CA39 AB03 10B8  F116 1713 1BCF 54C4 E1FE
Learn Python! http://www.ibiblio.org/obp/thinkCSpy