Desrtuctor WOES, was Advice on __del__

phil phillip.watts at anvilcom.com
Tue May 10 12:51:02 EDT 2005


> you haven't answered my question, btw: why are you using __del__
> to do something that the garbage collector should do for you?


After more reading it seems I have made an ass of my self on this
subject.  Here is my problem at length.

I teach high school geometry.  I have created a program with a
scalable graph on which to make drawings to illustrate concepts.
This is working well.


I like to MOVE the objects one pixel at a time or ROTATE them
one degree at a time so the students can follow the progress of
the lesson.

A script of a simple drawing might be:

scale 100
color red
tr1 = triangle from x y base baselength angles 30 60
rotate centerx centery degrees objlist   # eg tr1

This would create an instance of triangle which would
create an instance of polygon which would create 3
instances of lines, which are point pairs.

class line:
	def __init__(s,glob,argl,color=''):
	    s.glob = glob
	    :::
             :::

             x0 = a0  * s.glob.scale + 400
             y0 = 300 - b0  * s.glob.scale
             x1 = a1  * s.glob.scale + 400
             y1 = 300 - b1 * s.glob.scale
	    s.obj = glob.can.create_line(x0,y0,x1,y1,
					width=glob.width,fill=s.color)
	def __del__(s):
	    s.glob.can.delete(s.obj)

This calls Canvas.create_line.  Tkinter keeps its own list of
drawn objects.  I store the ref in s.obj

NOW when I rotate the triangle 45 degrees, recomputing the points
each time, it creates 45 new instances of triangle but dels the
old one.  __del__  then deletes the Canvas line object.

Without that, the list of objects grows very long,
and refresh gets slow.
This is a very complex program, 1000 lines now, propably 2000
lines eventually.  There are dozens of places where I depend on
__del__ deleteting canvas objects.

SO: without significant rewrite of each class such as triangle
and line, how can I ensure those canvas lines get deleted?
You don't really need to understand Canvas, just trust me
I have to delete those objects and they are not properties of the
class which go away with garbage collection.

Thanks.




More information about the Python-list mailing list