__del__ in classes derived from Tkinter classes

Andreas Kostyrka andreas at kostyrka.priv.at
Wed Jul 24 10:41:28 EDT 2002


Am Fre, 2002-07-19 um 13.12 schrieb Petr Klyushkin:
>  Fredrik> (and using __del__ is usually bad style even outside
>  Fredrik> Tkinter, but that's another story).
> 
> Why?
Well, actually because at best it's implementation dependant, at worst
it doesn't work at all. (And that's not really a bug, it's just how
liberal the Language Reference defines these things.) 
-) You do not have control of the destruction time. gc might destroy an
object at any time after it becomes inaccessible. The reference even
allows implementations not to destroy it at all.
-) Because you do not know when it is run, you cannot know what other
objects and services are still available. Basically the destruction
order is not defined.
-) So your options what you can do inside __del__ are quite limited.
   * It must be able to run at any time.
   * And the program must work without the __del__ handlers being
     called.

example:
class T:
	def __init__(self,v):
		self.v=v
	def __del__(self):
		print "DEL",self.v

def f():
	t1=T(1)
	t2=T(2)

f()

Results:
andreas at vaio2:~> python t2.py
DEL 2
DEL 1
andreas at vaio2:~> jython t2.py
andreas at vaio2:~>

Andreas





More information about the Python-list mailing list