reseting an iterator

Jan jan.plaza at plattsburgh.edu
Wed May 20 14:35:47 EDT 2009


Wouldn't it be easy for Python to implement generating functions so
that the iterators they return are equipped with a __reset__() method?

Here is the context of this question.

Python documentation defines  a "iterator" as an object ITERATOR
having methods __next__() and __iter__() such that the call
ITERATOR.__iter__() returns the object itself, and once a call
ITERATOR. __next__() raises StopIteration every such subsequent call
does the same.

Python iterators model "generating Turing machines", i.e.
deterministic Turing machines which take no input, which have a
separation symbol # in the output alphabet; have an one-way-infinite
output tape on which the output head prints and moves only to the
right; The machine may have a halting state; a word is said to be
"generated by M" if it appears on the output tape delimited by
separation symbols # (independently of whether M halts or computes
infinitely). Generating Turing machines provide a characterization of
recursively enumerable languages: a language is generated by a
generating Turing machine iff it is accepted by a Turing machine.

Turing machines can take as input and run other Turing Machines --
similarly Python functions can take other functions (including
iterators) as parameters and call them. HOWEVER, this symmetry breaks
down: a Turing machine which takes a generating Turing machine M as
input can run M for a number of steps and then RESET M and run it
again, while iterators as currently defined in Python do not have a
reset method. (I realize that instead of reseting an old iterator, one
can make a new iterator but it is not as elegant.)

(Contrary to Python's philosophy that there should be one-- and
preferably only one --obvious way to do it) there are several ways to
define iterators:



More information about the Python-list mailing list