Advice needed on __del__

André Roberge andre.roberge at gmail.com
Mon May 9 16:58:53 EDT 2005


Fredrik Lundh wrote:
> André Roberge wrote:
> 
> 
> 
>>This morning I had the following thought:
>>I can, when I create a Jekyll object, update
>>an outside_list.
>>If, somehow, I could update that list when
>>a Jekyll object disappears
>>(perhaps by using __del__, which I have never touched)
> 
> 
> Python makes no guarantees whatsover about when and how to
> run __del__, so relying on it to remove user-visible resources is a
> rather bad idea

[See idea at the very end about this.]

> 
>>Any suggestions?
> 
> why wouldn't
> 
>     def subplot():
>          Evil = Jekyll()   # Evil_twin gets created and displayed
>          Evil.doStuff()    # Evil_twin does stuff on the screen
>          Evil.destroy()
> 
> work for you?
> 
> </F>
Unfortunately, it wouldn't.
It's difficult to explain in a few words why, but I'll try anyway!

I'm developping a program (rur-ple) which is designed to
help learning Python.  In an earlier version (that didn't provide
support for deriving classes from existing one) I had it working so that 
when "Evil" would get out of scope, Evil_twin would disappear
from the screen.  This came as an initial surprise for me,
but a pleasant one.  Better than having an example like:

def boring():
    x = 1
    print x
boring()
print x  # gives an error message.

When I added the possibility of deriving classes, I had to
change the connection between Jekyll and Hyde ... and, so
far, lost that feature.

If I need to have the user call Evil.destroy() as Evil
is getting out of scope, it would miss the whole point
of teaching about the natural way scope and namespace
work.

I guess I'll have to try and learn how to use __del__,
and see if it can solve my problem.

===
A thought occurred to me, regarding "when" __del__
is called.  Can I force Python, through some function call,
to perform this.  Each time I refresh the screen, I could
force that call, then check to see if Evil has been
destroyed by Python, which would give me the information
I need to destroy Evil_twin behind the scene myself -
which is what I am really after.





More information about the Python-list mailing list