[Python-Dev] The iterator story

Oren Tirosh oren-py-d@hishome.net
Fri, 19 Jul 2002 09:23:51 -0400


> The Destructive-For Issue:
> 
>     In most languages i can think of, and in Python for the most
>     part, a statement such as "for x in y: print x" is a
>     non-destructive operation on y.  Repeating "for x in y: print x"
>     will produce exactly the same results once more.
> 
>     For pre-iterator versions of Python, this fails to be true only
>     if y's __getitem__ method mutates y.  The introduction of
>     iterators has caused this to now be untrue when y is any iterator.

The most significant example of an object that mutates on __getitem__ in
pre-iterator Python is the xreadlines object.  Its __getitem__ method 
increments an internal counter and raises an exception if accessed out of 
order.  This hack may be the 'original sin' - the first widely used 
destructive for.

I just wish the time machine could have picked up your posting when the
iteration protcols were designed. Good work.

Your questions will require some serious meditation on the relative 
importance of semantic purity and backward compatibility. 

	Oren