Singleton generators as code blocks

Greg Ewing (using g2h5dqi002 at
Mon Nov 24 06:41:37 CET 2003

Jp Calderone wrote:
>   This is a pretty nifty approach, but it seems to have (at least?) one serious
> drawback.  If the body of the loop raises an exception, the generator is
> never resume, and the cleanup (or whatever) is never run.

If open_file were implemented as a class rather than a
generator, maybe the cleanup could be implemented with its
__del__ method.

Or maybe the iterator protocol should include a __cleanup__
or __finally__ method that the for-loop invokes before leaving
via a break or exception?

Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand

More information about the Python-list mailing list