Iterators: Would "rewind" be a good idea?

Diez B. Roggisch deets at nospam.web.de
Sun May 21 18:23:26 EDT 2006


> My knowledge of Python's iterators is kind of sketchy, so I may have missed
> something.

The only thing a python iterator really is is something that supports a 
next()-method and will raise a StopIteration-Exception in case of 
exhaustion.

So - nobody stops you from introducing an object like

class Foo:
    def next(self):
        pass
    def prev(self):
        pass


and work with it as you like - the same way as in C++. But the syntactic 
sugaring and support in lots of classes that makes the iterator concept 
especially interesting in python makes only sense in one-way-iterators. 
Or do you really have a use-case for an reverting-in-the-middle for . in 
. - syntax?

And as Heiko explained (and you yourself mentioned too): 
forward/backward iteration makes only sense in case of a memory-backed 
implementation, namely containers. But the much more general concept of 
iterators of python applies to generators/generator expressions, 
itertools and so on.


Diez



More information about the Python-list mailing list