I have a problem in inheritance with python.
I need to override a python function and whenever that class called it should call the child method not the parent.
Here is the code :
in file called test_inherit.py
def get_uid(self, node):
from test_inherit import test_me
print "X is here",node
Subclassing changes the behavior of the child class but that has no effect on the parent. A class may have many inheritors and it wouldn't be clear which subclass should be called even if you could.
You can monkey-patch the parent as in the following example. I camel-cased the class name to help is stand out from variable names. The risk is that code called before the patching gets the old uid method.
class TestMe(object): def get_uid(self, node): return "parent" test1 = TestMe() uid1 = test1.get_uid() # monkey-patch to modify parent class def _my_get_uid(self, node): print "Parent hacked", node return "hack" TestMe.get_uid = _my_get_uid test2 = TestMe() assert uid1 == "parent", "cant change old calls" assert test1.get_uid(1) == "hack", "existing objects changed" assert test2.get_uid(1) == "hack", "new objects changed"