does python have useless destructors?

Ype Kingma ykingma at
Tue Jun 15 23:20:15 CEST 2004

David Turner wrote:

> "Delaney, Timothy C (Timothy)" <tdelaney at> wrote in message
news:<mailman.957.1087256687.6949.python-list at>...
>> David Turner wrote:
>> > This will be a pain for the Jython implementers.  However, it is
>> > doable.  Note that I never said the objects couldn't be garbage
>> > collected, just that   del   had to be called at certain well-defined
>> > times.  What this will involve is the Jython compiler inserting a lot
>> > of implicit try/finally constructs.
>> > 
>> > Can anyone see a reason why this scheme wouldn't work?
>> Yes - Jython cannot know about references to objects created *in Java
>> code*. It is therefore impossible for Jython to maintain reference
>> counts when an object is passed to Java (i.e. non-Python) code.
> Thank you, Tim, that's the first substantial objection I've heard :-).
> So, if Java code makes use of a Python object with __del__, all bets
> are off.  This is certainly a limitation, but I'm not sure that it's a
> very serious one, as RAII objects tend to be used in localized
> contexts.

It's not that all bets are off. In Jython the __del__() method is called
from java.lang.Object.finalize(), so you can place your bets there:

This javadoc begins with:
"Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object."

The closest thing to a guarantee is to explicitly call the garbage

>From Jython:

import java

I wouldn't bet against this, ie. the last time I tried the first call
to this gc() called all __del__() methods instantly on non referenced
Python objects.

> I think the only available behaviour is simply to ignore the
> possibility of an object being referenced externally.  Let Java code
> use Java idioms (try/finally).

For the moment, try/finally should also be a CPython idiom because
the call to __del__() is not guaranteed there either.

Kind regards,

email at

More information about the Python-list mailing list