How do I correctly clean up a Python object?



Cleanup happens to globals by setting them to None. The locals self destruct at the end of the session. The function __del__ called by Python sets the globals to None.

Consider the following code where there is clean up of all objects in the given class −

Example

class Counter:
    Count = 0   # This is the count of objects of this class
    def __init__(self, name):
        self.name = name
        print name, 'created'
        Counter.Count += 1
    def __del__(self):
        print self.name, 'deleted'
        Counter.Count -= 1
        if Counter.Count == 0:
            print 'Last Counter object deleted'
        else:
            print Counter.Count, 'Counter objects remaining'
x = Counter("First")
del x

Without the final del, you get an exception.

From the Python docs regarding __del__ −

Warning: Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. Also, when __del__() is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the __del__() method may already have been deleted. For this reason, __del__() methods should do the absolute minimum needed to maintain external invariants.

Without the explicit call to del, __del__ is only called at the end of the program, Counter and/or Count may have already been GC-ed by the time __del__ is called (the order in which objects are collected is not deterministic). The exception means that Counter has already been collectd. You can’t do anything particularly fancy with __del__.

There are two possible solutions here.

  • Use an explicit finalizer method, such as close() for file objects.

  • Use weak references.


Advertisements