People are often confused about Python's
del keyword and
Most people think that applying
del on an object causes the object's
__del__() method to be called. But this is not true.
del keyword merely
decrements the objects reference count and de-scopes the variable on its
application. But it doesn't necessarily invoke the object's
__del__ method will be called only when the garbage collection kicks in.
And this would happens automatically when the reference count of an object
del: Decrements reference count
__del__(): Object destructor. Called when an object is garbage collected
class Foo(): def __del__(self): print '__del__() called'
Now let's create some instances of this
Foo class and call
del upon these
instances to see what happens.
In : %paste class Foo(): def __del__(self): print '__del__() called' ## -- End pasted text -- In : from sys import getrefcount In : getrefcount(Foo) Out: 2 In : a = Foo() In : getrefcount(a) Out: 2
At this point you might be wondering why
shows 2 instead of 1. Well, the reason is when you call the method
the argument is passed by value, effectively bumping the reference count by 1.
This extra reference will be freed up once the
getrefcount method exits. So
effectively we have a reference count of 1 for the instance
a here. Now let's
make more references to the same instance.
In : b = a In : getrefcount(a) Out: 3 In : getrefcount(b) Out: 3
As expected, the reference count is incremented. Now let's call
del on a to
see if the object's
__del__() is invoked.
In : del a In : del b __del__() called
As you can see,
__del__() is not invoked on applying
del the first reference.
This is because the reference count is still 1. On calling
the reference count become zero, garbage collection kicks in and finally
our long awaited
__del__() is called.
But there are situation where the destructor goes astray. These situations and the alternative approaches are explained in detail in the following brilliant blog post.