Finally figured out member generators...

Greg Ewing (using me at
Tue Mar 11 03:52:04 CET 2003

Bjorn Pettersen wrote:
> Well, yes, I guess I gave it away when I said "the whole point was to
> have the class be it's own iterator" <wink>. Although it would certainly
> cause problems with multiple simultaneous iterations over the same
> container, there shouldn't be anything wrong with doing it when you know
> there can only be one for loop (etc.) working on the container.

But if you're using a generator to do the iterating,
you're (implicitly) creating a separate iterator
object anyway, so there's no point in trying to make
the class be its own iterator.

There would only be a point to it if you *weren't*
using a generator, and explicitly maintaining the
iteration state yourself, in which case it might
be marginally simpler to keep that state in the
iterable instead of a separate iterator object.

But I think I would only be inclined to do that if
the object was by its very nature only iterable

Particularly since generators now make it so easy
to implement separate iterators properly!

Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand

More information about the Python-list mailing list