Weakref.ref callbacks and eliminating __del__ methods

Richie Hindle richie at entrian.com
Tue Jan 25 05:03:02 EST 2005


[Tim]
> there's no 100% guarantee that a __del__ method will ever get called

Can anyone point me to any definitive documentation on why this is the
case?  I was under the impression that __del__ would always be called:

 1. for objects not in cycles and with no references

 2. for objects whose only references are from cycles which themselves
    have no external references and no __del__methods, assuming that
    garbage collection runs.

> __del__ methods (weakref callbacks too, for that matter) triggered
> while Python is tearing itself down at exit may suffer bizarre
> exceptions (due to trying to use facilities in partially-torn down
> modules, including the module the __del__ method appears in).

Good point.

> In general, __del__ is a user-visible method like any other, and user
> code may call it explicitly.  For that reason, it's safest to write
> __del__ methods in library objects such that they can be invoked
> multiple times gracefully.

Another good point - thanks.

-- 
Richie Hindle
richie at entrian.com




More information about the Python-list mailing list