[Python-Dev] Single- vs. Multi-pass iterability

David Abrahams David Abrahams" <david.abrahams@rcn.com
Mon, 15 Jul 2002 20:25:38 -0400


From: "Andrew Koenig" <ark@research.att.com>


> Ping> Just fetch the iterator from the container and look for __copy__ on
that.
>
> Yes, that's an alternative.
>
> However the purpose my suggestion of __multiter__ was not to use it to
> test for multiple iteration, but to enable a container to be able to
> yield either a single or a multiple iterator on request.

Why would you want that? Seems like a corner case at best.

> A data structure that supports several different kinds of iteration
> has to provide that support somehow.  What's your suggestion?

class DataStructure(object):
    def __init__(self):
        self._numbers = range(10);
        self._names = [ str(x) for x in range(10) ];

    names = property(lambda self: iter(self._names))
    numbers = property(lambda self: iter(self._numbers))

x = DataStructure();
for y in x.names:
    print repr(y),

print

for y in x.numbers:
    print repr(y),

[Y'know, Python is great. That worked the first time I ran it.]

-Dave