Resources and non-exhausted generators

Sean 'Shaleh' Perry shalehperry at attbi.com
Thu May 2 11:16:49 EDT 2002


On 02-May-2002 Hamish Lawson wrote:
> I've written a generator (slightly modified below) that returns results
> from a database. Normally this is intended to be run from a for-loop
> that will exhaust the produced results. However it occurred to me that
> if the generator is not run to completion, then the cursor and
> connection will not get properly closed. I can't see any way to do this
> using a generator, and I've come to the conclusion that I must
> reengineer this as an iterator with a __del__ method that can tidy up
> resources (with probably also an explicit method for allowing this to be
> done earlier than object destruction). Is my analysis correct?
> 

'del object' will do it earlier (-:






More information about the Python-list mailing list