How to overide "for line in file:"

Terry Reedy tjreedy at udel.edu
Mon Feb 23 10:57:51 EST 2004


"Harry George" <harry.g.george at boeing.com> wrote in message
news:xqxsmh2dyyn.fsf at cola2.ca.boeing.com...
> Slightly off topic, how do you "reset" or backup an iter?

Write your own bidirectional iterator class with a backup or prev method
and use it within a while loop instead of for loop.  Iterators produced by
iter() and generator functions are meant to be once through.  This
simplicity allows very efficient yield/next control tranfers.  Some
iterables, such as actual sequences, can be reiterated from the beginning.
And generator functions can to re-called, with same or different params.

> Historically, I've made a list, kept track of the line index, and
> decremented that when I needed to backup.  Does iter have something
> equiv?

If you mean builtin iter(), no.

Terry J. Reedy







More information about the Python-list mailing list