strange behaviour at termination time

Greg Ewing greg.ewing at compaq.com
Mon Jun 28 22:59:17 EDT 1999


Fabien COUTANT wrote:
> 
> If you change the name of the first class from A to X everywhere
> then X is bound to None in B.__del__ !!!

When the Python interpreter is shutting down, it
goes through all the loaded modules and sets all
the names defined in them to None. It does this in
an arbitrary order, and it appears that in this
case it happens to delete the name "A" after
"y", but "X" before "y".

To avoid problems like this, you have to design
your __del__ methods so that they can do their
work without having to refer to any module-level
names at the time they are called. One way is
to use the default-argument trick:

class B (A) :
    def __del__ (self, A = A) :
        A.__del__ (self)

Greg




More information about the Python-list mailing list