[Python-iterators] Re: PEP 234: Iterators

Thomas Bellman bellman at lysator.liu.se
Tue May 1 15:01:05 EDT 2001


Just van Rossum <just at letterror.com> wrote:

> I think it will be rare for iterators created "manually" with iter(obj) to
> be passed "manually" to a for loop.

And just to contradict you, my very first use of Python iterators
did exactly that.  Of course, my iterator had special methods
set_start() and set_end() in order to make it start at a certain
position in the sequence and end at another.

My program was to download a set of web pages, indexed by a date
in the URL.  The next() method in the iterator generated the URL
for the next day, and I didn't want it to start trying to download
2000 years worth of web pages...  Thus I would do:

    ui = iter(obj)
    ui.set_start(2000,4,23)
    ui.set_end(2000,5,29)
    for url in ui:
	urlopen.urlretrieve(url)

to download the pages pertaining to dates between April 23rd and
May 29th, inclusive, last year.  (Actually, it is a bit more
complicated than that, but at least that is the gist of it.)


-- 
Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"Too much of a good thing is WONDERFUL."     !  bellman @ lysator.liu.se
                             -- Mae West     !  Make Love -- Nicht Wahr!



More information about the Python-list mailing list