Today we had to patch a class function in production. Monkey patching can become tricky if reference are kept at several place like pointer in C and C++.
Here is a simple example on how to make sure all references will use the new definition.class Foo:This is another reason why interpreter language like python are so powerful.
def f(self):
print "default f"
def newf(self):
print "newf"
Foo.f.im_func.func_code = newf.func_code
Some thoughts of a Machine Learning Practitioner on Software Development, Management, Team Building, Startups, Python, Agile Development, Data visualization... that will distract you from your end goals by making you less efficient but are critical to manage in order to succeed. Don't forget that long time adaptation to inefficient approaches can become your enemy. Let's try to empower others by sharing knowledge & personal experiences.
Wednesday, July 21, 2010
patching class function in python
Subscribe to:
Posts (Atom)