Is this a bug with __del__()

Alex Martelli aleaxit at yahoo.com
Fri May 25 10:24:56 CEST 2001


"Greg Ewing" <see at my.signature> wrote in message
news:3B0DBEE6.1AFE90FB at my.signature...
> Costas Menico wrote:
> >
> > I wanted to use the __del__ method in a larger example class
>
> You should be able to design your example so that
> the last reference to your instance goes away at
> a predictable moment, e.g.
>
>   print "Creating an instance"
>   x = ClassFoo()
>   print "Discarding the instance"
>   x = None
>   print "Finished"
>
> That gives:
>
> Creating an instance
> Discarding the instance
> Delete called
> Finished

...in the current version(s) of Classic Python, but not
necessarily in Jython, Python.NET, or (if my understanding
is correct) in some possible future version(s) of Python.

I wouldn't rely on __del__ necessarily getting called as
soon as the last reference goes away... try/finally is
more reliable and portable if you need _guaranteed_
finalization opportunities.


Alex






More information about the Python-list mailing list