Destructors and exceptions

Terry Reedy tjreedy at udel.edu
Mon Jun 7 11:26:44 EDT 2004


"David Turner" <dkturner at telkomsa.net> wrote in message
news:e251b7ba.0406070651.1c98c09d at posting.google.com...
> The problem is that when an exception is raised, the destruction of
> locals appears to be deferred to program exit.  Am I missing
> something?

When the last reference to an object disappears, the interpreter *may* but
is *not required* to forget or destruct the object, if indeed the concept
is meaningful.  What happens is implementation dependent.  CPython usually
cleans up immediately, which is sooner than with Jython.  I am not sure
what human interpreters do.  Write once, never erase storage of objects
(keeping a complete audit trail of objects created) would also be legal.

If you want to force the issue, give your class a close method, as with
file objects, and call it explicitly.  Then code in try:finally:, which is
designed for this purpose.
try:
   process
finally:
   cleanup

Terry J. Reedy







More information about the Python-list mailing list