Destructor Woes, was Advice needed on __del__

flupke flupke at nonexistingdomain.com
Thu May 12 03:06:23 EDT 2005


phil wrote:
> 
>> Then i got a tip that you can register a function that needs to be 
>> called when the object is going to be deleted.
>> For instance to register a function __exit, you do this:
>>
> 
> 
> Here is the complete line class with your suggestion:
> 
> Below is the output.
> 
> Nice idea, maybe I did something wrong?
> 
> class line:
>     def __init__(s,glob,argl,color=''):
>         atexit.register(s.__exit)
>         s.glob = glob
>         s.type = 'line'
>         s.color = color
>         if not s.color: s.color = glob.color
>         if len(argl) == 2:
>             wobj = glob.objs[ argl[0] ][0]
>             s.x0 = a0 = wobj.x
>             s.y0 = b0 = wobj.y
>             wobj = glob.objs[ argl[1] ][0]
>             s.x1 = a1 = wobj.x
>             s.y1 = b1 = wobj.y
> 
>         elif len(argl) == 4:
>             s.x0 = a0 = float( argl[0] )
>             s.y0 = b0 = float( argl[1] )
>             s.x1 = a1 = float( argl[2] )
>             s.y1 = b1 = float( argl[3] )
>         else: return
>         s.midx = (s.x0 + s.x1) / 2
>         s.midy = (s.y0 + s.y1) / 2
>         s.len = sqrt( (s.x1 -s.x0)**2  + (s.y1 -s.y0)**2 )
>         if  (s.y1 -s.y0) == 0: s.slope = 0
>         elif  (s.x1 -s.x0) == 0: s.slope = 999999
>         else: s.slope = (  (s.y1 -s.y0) / (s.x1 -s.x0) )
> 
>         # center point of graph is 400,300
>         # scale is no of pixels per unit of measure
>             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 __exit(s):
>         print 'exit'
>         s.glob.can.delete(s.obj)
> 
>     # def __del__(s):
>     #     s.glob.can.delete(s.obj)
> 
> 
> exit
> 
> Error in sys.exitfunc:
> Traceback (most recent call last):
>   File "/usr/local/lib/python2.3/atexit.py", line 20, in _run_exitfuncs
>     func(*targs, **kargs)
>   File "./graph.py", line 972, in __exit
>     s.glob.can.delete(s.obj)
>   File "/usr/local/lib/python2.3/lib-tk/Tkinter.py", line 2085, in delete
>     self.tk.call((self._w, 'delete') + args)
> _tkinter.TclError: invalid command name ".1076354284"
> 

I don't know what's wrong in your example. Here it works (doesn't help 
you of course). I would first try with an easy example an try to test 
the atexit functionality like that. It seems that the problem you are 
having isn't related to the atexit part.
Sorry i couldn't be of more help

Benedict



More information about the Python-list mailing list