When an instance variable of a class with the same
name as a class variable in a base class is assigned
a value (making the class variable of the base class
invisible), the class instance does not appear to be
destructed when it should.
Here is the simplest test script I've been able to
come up with that reproduces the error, along with
its output when run from a shell prompt. I've
included the dir() commands to make sure that the
variable referencing the class instance is in fact
deleted in both cases. As you can see, the instance
of the base class gets destructed as expected, but
the instance of the derived class does not.
--- Test script ---
#!/usr/bin/env python
# Test script to see when objects are freed
class Test(object):
testfield = None
def __init__(self):
print "Initializing test object."
def __del__(self):
print "Freeing test object."
class Test2(Test):
def __init__(self):
# This masks Test.testfield
self.testfield = self.meth
Test.__init__(self)
def meth(self):
pass
print dir()
t = Test()
print dir()
del t
print dir()
t2 = Test2()
print dir()
del t2
print dir()
--- Output ---
$ python deltest.py
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func']
Initializing test object.
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func', 't']
Freeing test object.
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func']
Initializing test object.
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func', 't2']
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func']
|