Iterators vs. Generators

Hamish Lawson hamish_lawson at yahoo.co.uk
Tue Jun 11 04:51:40 EDT 2002


aahz wrote:

> So when would one actually write an iterator instead of a generator?

When you need to ensure that any allocated resources get cleared up
even if the iteration is abandoned early. This can be handled by an
iterator that has a clear-up method and a __del__ method that calls
that clear-up method. That gives you the option of explicitly
initiating the clear-up immediately on abandoning an iteration, or
else leaving the clear-up to be done automatically when the iterator
object is destroyed.

Hamish Lawson



More information about the Python-list mailing list