reseting an iterator
J. Clifford Dyer
jcd at sdf.lonestar.org
Fri May 22 20:31:22 EDT 2009
On Fri, 2009-05-22 at 10:54 -0700, Jan wrote:
> This produces an error because by definition of for-loops
> it is executed the same way as:
>
> temp_iterator = iter(y) # temp_iterator is y
> while True:
> try:
> print(next(temp_iterator)) # temp_iterator does not support
> __next__()
> except StopIteration:
> break
>
I think this is where you missed my point.
iter(y) actually returns an instance of class X, which does support
iteration. And it returns a new X each time, thus resetting the
iterator.
That exact setup might or might not support your use case. I don't
know, because you haven't described it. However, whatever you need done
to X to get it back in shape to reiterate over can be done in
Y.__iter__().
Honestly, do you care if it's an iterator or an iterable, so long as
python can handle the job?
More information about the Python-list
mailing list