[Tutor] destructor methods

Lloyd Kvam pythontutor@venix.com
Fri, 30 Nov 2001 16:23:19 -0500


C++ puts responsibility for heap memory management on the programmer.  new
will grab some memory and del will release.

In converting to Python:
	x = new Class;	==>	x = Class()
	(that c++ code is from memory.  It may be slightly off)
The del statements can be discarded.

__del__ exists for odd situations.  One example, if your class creates
doubly linked lists, you could have:
	a.next = b
	b.prior = a
	del a; del b
Python's old garbager collect will NOT collect this memory because those
two deleted each reference each other.  I beleive there are PEP's to improve
this, but don't know where they stand.  These deletions also break the list!
You would use __del__ to come up with logic to handle these kinds of cases
AND also protect the linked list so that deletions didn't break the list.

Timothy Wilson wrote:

> 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__?
> 
> -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
> 
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice: 
603-443-6155
fax: 
801-459-9582