Problem with __del__

Fredrik Lundh fredrik at effbot.org
Tue Jan 9 14:39:41 EST 2001


Stephan Effelsberg wrote:
> Now I swapped the lines for instanciating 'cl' and 'nothing' with the
> following result:
>
> cl2 <function dosomething at 8a7c90>
> doing something
> cla <function dosomething at 8a7c90>
> doing something
> nothing None
> Exception exceptions.TypeError: 'call of non-function (type None)' in
> <method Nothing.__del__ of Nothing instance at 8a7ee0> ignored
> cl None
> Exception exceptions.TypeError: 'call of non-function (type None)' in
> <method Nothing.__del__ of Nothing instance at 8a7f60> ignored
>
> Now it seems that both 'nothing' and 'cl' aren't able to see the
> function dosomething().
>
> Can somebody explain this phenomenon?

python replaces global objects with None during cleanup, in a well-
defined but somewhat unpredictable (for a human, at least) order:

    http://www.python.org/doc/essays/cleanup.html

if you can, get rid of the destructors (__del__ methods are evil, and
shouldn't be used in normal programming).

if you really must keep them around, make sure you keep references
to global objects:

    def __del__(self, dosomething=dosomething):
        print self.name, dosomething
        dosomething()

hope this helps!

cheers /F





More information about the Python-list mailing list