Iterators (was: Re: [Tutor] text module)

Erik Price erikprice@mac.com
Sat, 31 Aug 2002 08:36:18 -0400


On Friday, August 30, 2002, at 07:41  PM, Scot W. Stevenson wrote:

> This is okay if you know that the file is going to be small and you 
> machine
> is big, but for large files on small machines, it could be a problem:
> readlines loads the whole file into memory in one big gulp. xreadlines 
> was
> created in Python 2.1 (I think) to avoid this problem, but if you have
> Python 2.2 or later, the really cool thing to do is to use iterators 
> and
> simply create a loop such as:
>
> for line in subj:
>     (etc)

Not having any idea what iterators are, I went to the docs 
(http://python.org/doc/current/whatsnew/node4.html) and read about 
them.  But as always I am not sure of everything.

I understand the construction that Scot uses above, because the docs 
explain that file objects incorporate the __iter__() method, and "for" 
loops now act upon this method for all sequence objects (and file 
objects too).

It's the first part of the explanation of iterators that I'm not 
totally clear on, the part that explains how you can incorporate your 
own iterators into your own class definitions.


Is it that you specify an "__iter__()" method, and then you implement 
the code that would go about returning a "next" item each time the 
__iter__() method is called?  How do you store the state of the object 
such that it knows which item to return next time the __iter__() method 
is called?  Am I correct in assuming that you must implement the 
StopIterator exception yourself in the code for situations in which 
there are no more items to be iterated over?  And finally, do you write 
a separate class definition for an iterator, and then have your 
original class definition use the iterator object, or is this something 
that can be entirely kept within the class definition for the object in 
question?

It's a confusing document IMHO.  I would appreciate any discussion and 
thoughts that anyone cares to contribute.



Erik





--
Erik Price

email: erikprice@mac.com
jabber: erikprice@jabber.org