Monkey patch __del__ to new function
del a
deletes the name 'a' from the namespace, but not the object referenced by that name. See this:
>>> x = 7
>>> y = x
>>> del x
>>> print y
7
Also, some_object.__del__
is not guaranteed to be called at all.
Also, I already answered your question here (in german).
While special 'double underscore' methods like __del__
, __str__
, __repr__
, etc. can be monkey-patched on the instance level, they'll just be ignored, unless they are called directly (e.g., if you take Omnifarious's answer: del a
won't print a thing, but a.__del__()
would).
If you still want to monkey patch a single instance a
of class A
at runtime, the solution is to dynamically create a class A1
which is derived from A
, and then change a
's class to the newly-created A1
. Yes, this is possible, and a
will behave as if nothing has changed - except that now it includes your monkey patched method.
Here's a solution based on a generic function I wrote for another question: Python method resolution mystery
def override(p, methods):
oldType = type(p)
newType = type(oldType.__name__ + "_Override", (oldType,), methods)
p.__class__ = newType
class Test(object):
def __str__(self):
return "Test"
def p(self):
print(str(self))
def monkey(x):
override(x, {"__del__": p})
a=Test()
b=Test()
monkey(a)
print "Deleting a:"
del a
print "Deleting b:"
del b