[Python-Dev] Single- vs. Multi-pass iterability
David Abrahams
David Abrahams" <david.abrahams@rcn.com
Mon, 15 Jul 2002 20:39:22 -0400
From: "Andrew Koenig" <ark@research.att.com>
> >> A data structure that supports several different kinds of iteration
> >> has to provide that support somehow. What's your suggestion?
>
> David> class DataStructure(object):
> David> def __init__(self):
> David> self._numbers = range(10);
> David> self._names = [ str(x) for x in range(10) ];
>
> David> names = property(lambda self: iter(self._names))
> David> numbers = property(lambda self: iter(self._numbers))
>
> David> x = DataStructure();
> David> for y in x.names:
> David> print repr(y),
>
> David> print
>
> David> for y in x.numbers:
> David> print repr(y),
>
> David> [Y'know, Python is great. That worked the first time I ran it.]
>
> I don't understand how this code answers my question.
> You've asked for iterators over two different data structures.
> What I was asking was, for example, how one might arrange for a single
> tree to yield either a depth-first or breadth-first iterator.
Just replace 'names' by breadth_first and 'numbers' by depth_first.
or-vice-versa-ly y'rs,
dave