does python have useless destructors?

Terry Reedy tjreedy at udel.edu
Thu Jun 10 07:51:17 CEST 2004


"Donn Cave" <donn at u.washington.edu> wrote in message
news:donn-BCE10C.15003209062004 at nntp3.u.washington.edu...
> In article <slrnccetd9.gag.msoulier at tigger.digitaltorque.ca>,
>  "Michael P. Soulier" <msoulier at digitaltorque.ca._nospam> wrote:
>
> > I recently had a friend tell me that because the Python spec does not
> > guarantee that an object will necessary be destroyed when all
references
> > to it are gone, that desctructors in Python are not reliably called,
and
> > thus useless.

To me, this is useless over-dramatization ;-) Is your car useless because
there is no guarantee that the engine will never quit?  or the brakes fail?

While aspects of memory management are system dependent and outside of
Python's control, programs running on standard hardware with the CPython
interpreter do what one can expect: memory is freed for reuse by the
interpreter (but not necessarily for resuse by other programs, as some
mistakenly expect).

>>  As I found it difficult to believe that Python would
> > include destructors for apparently no reason,

And they are not.  Under the conditions stated above, deletion works.  But
Guido did not want to restrict Python to C implementation on standard
systems.

> -  Reference cycles can prevent immediate finalization.
>    If an object holds a reference to itself, however
>    indirectly, it won't be deleted automatically and has
>    to be recovered through garbage collection, and garbage
>    collection won't delete instances with a __del__ method,
>    among other things.

This is admittedly a nuisance.  Howver, the original solution was reference
counting only.  Programmers had to explictly break loops to reclaim
resources.  This option remains available.

When cyclic gc was introduced, I believe there was no such restriction.
The result was indeterminate chaos and sometime bad behavior.  After trying
several fixes, the present rule was introduced.  Of course, this made
cyclic gc less useful than intended, but seg faults are a no-no.  No one
has volunteered anything better since.

Terry J. Reedy







More information about the Python-list mailing list