[Python-Dev] Single- vs. Multi-pass iterability

Oren Tirosh oren-py-d@hishome.net
Fri, 12 Jul 2002 16:17:31 +0300


On Fri, Jul 12, 2002 at 08:46:26AM -0400, Guido van Rossum wrote:
> > I have just submitted a patch that makes a file into an iterator
> > (i.e. adds a .next method to files).  With this change all Python
> > objects that have an __iter__ method and no next method produce
> > iterators that do not modify the container.  Another possibility
> > would be to make file iterators that use seek or re-open the file to
> > avoid modifying the file position of the parent file object.  I
> > don't think that would be a good idea because files can be devices,
> > pipes or sockets which are not seekable.
> 
> Cute trick, but I think it's too fragile.  You don't know about 3rd
> party iterables that have the same problem as file.

I don't understand what you mean by fragile. I'm not suggesting anything
that actually depends on this behavior so I don't see what could break.

I think it's semantically cleaner for iterable objects to produce iterators 
that do not modify the state of the original iterable object. There's no 
way to force extension writers to adhere to this but Python should at least 
set a good example. Python file objects are not a good example. The xrange
object that was its own iterator was not a good example.

	Oren