[Tutor] destructor methods

Andrew Wilkins toodles@yifan.net
Sat, 1 Dec 2001 02:03:08 +0800


> On Sat, 1 Dec 2001, Andrew Wilkins wrote:
>
> > When defining a class, you can define a method called __del__ to handle
what
> > happens in the deletion, so it has the desired effect. Look for more in
> > "3.3.1 Basic Customisation" in the Python Reference Manual.
>
> So is there a performance benefit to explicitly calling __del__?

Sorry I should have explained a little better (it's 2 in the morning, heh).
__del__ is called just before the object is to be deleted - it doesn't
delete the object, but rather it performs some actions before the object is
deleted. And also, the reference counting needs to be taken into account.
__del__ isn't called until the object is deleted, ie. when the reference
count is 0. I'll show a quick demo:

>>> class x:
...  def __del__(self):
...   print "Destruction!"
...
>>> y=x()
>>> z=y
>>> del y
>>> del z
Destruction!


Now I'm not sure if I answered your initial questions at all...
Maybe I'll look at that C code after some sleep, and see if I can see
anything that destructors are useful/beneficial for.
HTH
Andrew

>
> -Tim
>
> --
> Tim Wilson      |   Visit Sibley online:   | Check out:
> Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
> W. St. Paul, MN |                          | http://slashdot.org
> wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>