Iterators: Would rewind be a good idea?
I was reading through old messages in the list and came up against an idea that I thought might be of some value: Wouldn't it be a good idea if one could rewind an iterator? Not stated in precisely those terms, perhaps, but that's the way I read it. I appreciate that one could use a sequence rather than an iterator, and that I don't understand the implementation issues. (They don't look large, but what to I know?) But would it be a good idea to have a rewind method for iterators? I tend to think of them as mag tapes, and I was always wanting to rewind those. Or skip to file n. (Well, that probably doesn't have any relevance to the usage of iterators.) OTOH, if this is a bother, it's almost always quite easy to create a new iterator, it just feels less efficient. Still, I would use this much more often that I would use reversed (which I've never wanted to use). -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterators: Would rewind be a good idea?
Am Sonntag 21 Mai 2006 21:43 schrieb Charles D Hixson: I was reading through old messages in the list and came up against an idea that I thought might be of some value: Wouldn't it be a good idea if one could rewind an iterator? Not stated in precisely those terms, perhaps, but that's the way I read it. Yes, that certainly would be a neat idea. But, think of the following: what if the iterator computes the values at runtime, and you're not iterating over a predefined list of some sort? Do you want the machinery to store the state of the iterator at every earlier point in time (sometimes this may not even be possible, think of socket communication handled by iterators, or of some state being kept in a C extension outside of the grasp of the Python interpreter), and to restore the iterator to this point? Even if the generator you're trying to rewind can be pickled at each yield statement, memory requirements come to mind when thinking about this for the general case, where you simply don't want to rewind, but just iterate forward. Anyway, an easy way out (if the aforementioned concerns don't apply) is always to create a list of the generator, and then to index that list directly: x = list(gen()) A list can be freely indexed, so basically if you implement the iterator logic using a while loop, you're free to rewind as much as you'd like. --- Heiko. -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterators: Would rewind be a good idea?
Heiko Wundram wrote: But, think of the following: what if the iterator computes the values at runtime, and you're not iterating over a predefined list of some sort? Do you want the machinery to store the state of the iterator at every earlier point in time (sometimes this may not even be possible, think of socket communication handled by iterators, or of some state being kept in a C extension outside of the grasp of the Python interpreter), and to restore the iterator to this point? This is why the C++ STL has independent forward and backward iterator types. Iterating over containers in memory generally supports both. Iterating over stdin or a socket would only support forward iteration. I can't recall any examples of backwards-only iteration atm... maybe popping values off a stack. My STL's a bit rusty. Anyway, python iterators could provide a prev() method to go with next(). If the iterator doesn't support backwards iteration (sockets, generators), it could raise an exception. Then you could implement rewind as: def rewind (iter) try: while iter.prev(): pass except StopIteration: pass There's no faster way in general to rewind an iterator (think linked lists), just like there's no fast_forward() to automatically jump current iterators to the end of the sequence (which may not terminate anyway, a la itertools.count). Note that backwards iterators are different from iterating in reverse. Namely, the former let's you move back and forth at will in a sequence, while the latter is just a one-way street from back to front. My knowledge of Python's iterators is kind of sketchy, so I may have missed something. -- Edward Elliott UC Berkeley School of Law (Boalt Hall) complangpython at eddeye dot net -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterators: Would rewind be a good idea?
Edward Elliott [EMAIL PROTECTED] wrote: This is why the C++ STL has independent forward and backward iterator types. Let me see if I can paraphrase the difference between the Python design philosophy and the C++ design philosophy about most things. Python says, Let's make things simple enough that it can be explained fully in a couple of paragraphs, even if it means there are some things you can't do. C++ says, Let's make it possible to do anything, even if it makes it complicated enough that it takes a shelf full of books to explain it all. There may be value in restartable iterators, but comparing what we've got now to STL isn't the way to convince people it's a good idea :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterators: Would rewind be a good idea?
Roy Smith wrote: Edward Elliott [EMAIL PROTECTED] wrote: This is why the C++ STL has independent forward and backward iterator types. Let me see if I can paraphrase the difference between the Python design philosophy and the C++ design philosophy about most things. Python says, Let's make things simple enough that it can be explained fully in a couple of paragraphs, even if it means there are some things you can't do. C++ says, Let's make it possible to do anything, even if it makes it complicated enough that it takes a shelf full of books to explain it all. STL has its problems, complexity among them, but just because it's in the STL doesn't automatically make it a bad idea. They faced a similar problem, and this is how some very smart people solved it. There's always value in exploring that. It doesn't get much simpler than adding one method (prev) with symmetry to an existing method (next). There may be value in restartable iterators, but comparing what we've got now to STL isn't the way to convince people it's a good idea :-) BTW I never said backwards iterators were a good idea. I haven't decided yet. -- Edward Elliott UC Berkeley School of Law (Boalt Hall) complangpython at eddeye dot net -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterators: Would rewind be a good idea?
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 -- http://mail.python.org/mailman/listinfo/python-list