Deterministic destruction and RAII idioms in Python

Paul Rubin http
Mon Jan 30 18:47:41 EST 2006


plahey at alumni.caltech.edu writes:
> Giving up deterministic destruction in Python would be a real blow for
> me, since it is one of its unique features among GC'ed languages.
> 
> So what's the deal, can I rely on it in "mainstream" Python or am
> I out of luck here?

IMO you shouldn't rely on it.  I believe it already doesn't work in
Jython, which uses the regular Java GC system.  See PEP 343 for a new
(forthcoming) Python mechanism to do what you're looking for:

  http://www.python.org/peps/pep-0343.html

You shouldn't think of reference counting as being like C++
destructors.  A destructor definitely runs when the C++ object goes
out of scope.  Ref counting frees only the object if there are no
other references when the scope exits.  So if you want to rely on the
object getting gc'd when you expect, you have to carefully manage the
references, which defeats the point of the language doing it for you
automatically.



More information about the Python-list mailing list