[Tutor] __del__ exception

Alan Gauld alan.gauld at btinternet.com
Fri Jun 6 00:02:17 CEST 2008

"Blaise Morose" <jdeambulemorose at gmail.com> wrote

> I have this silly piece of code that I am experimenting with:

I'm not certain but... based on this quote from the Python docs:

Warning: Due to the precarious circumstances under which __del__() 
methods are invoked, exceptions that occur during their execution are 
ignored, and a warning is printed to sys.stderr instead. Also, when 
__del__() is invoked in response to a module being deleted (e.g., when 
execution of the program is done), other globals referenced by the 
__del__() method may already have been deleted. For this reason, 
__del__() methods should do the absolute minimum needed to maintain 
external invariants. Starting with version 1.5, Python guarantees that 
globals whose name begins with a single underscore are deleted from 
their module before other globals are deleted; if no other references 
to such globals exist, this may help in assuring that imported modules 
are still available at the time when the __del__() method is called.

> class Person:
> p = Person('Jean')
> d = Person('Michael')

Because you don't explicitly kill the objects the interpreter may have
deleted the class object before the instance  __del__ gets called.
What happens iof you explicity del(p) and del(d)?

Just a thought.

Alan G. 

More information about the Tutor mailing list