Destructors

Terry Reedy tejarex at yahoo.com
Sat Apr 6 10:38:31 EST 2002


"Robert Oschler" <Oschler at earthlink.net> wrote in message
news:Y7Ar8.5367$Tf4.2431098 at news2.news.adelphia.net...
> My favorite object construct/destruct paradigm is the Boost
Library's
> shared_ptr template class for C++ objects.  When the reference count
of the
> shared pointer reaches zero, the destructor of the object it wraps
is
> called.  Delphi has "interfaces" which do the same, although they
behave
> differently than classes do when it comes to inheritance and other
subtle
> programming aspects so it can be a little awkward at times.
>
> Python seems to follow the Java finalize/gc() model where
destructors (or
> finalize() in Java) aren't called until the garbage collector frees
the
> object.

The language standard does not specify when destructors/finalizers are
called.  The standard CPython implementation uses reference counting
(with optional gc to clean up circular references).  The Jython
(Python in Java instead of C) implementation does use the Java model.

 >As in Java, the Python book I'm reading stresses that you have no
> idea when the destructor will be called, or if it will happen at
all.

If your code will or might be run on Jython, then this is true.

Terry J. Reedy






More information about the Python-list mailing list