我正在尝试学习 python 猴子补丁。我有一个简单的示例,其中我试图仅对单个实例而不是类本身进行猴子修补。
我的代码:
# add.py
import types
class Math(object):
def __init__(self):
self.name = ''
def add(self, x, y, name):
self.name = name
print 'calling from ', self.name
return x + y
def monkey_patch(self):
add = self.add
def squared_sum(x, y):
return x**2 + y**2
add = types.MethodType(squared_sum, self)
if __name__ == '__main__':
math = Math()
print math.add(3, 4, 'before monkey_patching')
math.monkey_patch()
print math.add(3, 4, 'after monkey_patching')
预期输出:
calling from before monkey_patching
7
calling from after monkey_patching
25
生成的输出:
calling from before monkey_patching
7
calling from after monkey_patching
7
谁能指出我哪里出错了。还有,当我从不同的文件执行添加方法时,如何猴子修补它,即当我从不同文件中的 add.py 导入 Math 类时,我如何猴子修补它的添加方法。
最佳答案
您的代码没有按照您的想法行事:
def monkey_patch(self):
add = self.add # add now points to self.add
def squared_sum(x, y):
return x**2 + y**2
add = types.MethodType(squared_sum, self) # add now points to squared_sum
# method ends, add and squared_sum are abandoned
这实际上并没有改变 self.add
.另外,squared_sum
不需要 self
或 name
参数,不像add
,并且没有 print
那add
做。要使这项工作充分发挥作用,请执行以下操作:
def monkey_patch(self):
def squared_sum(self, x, y, name):
self.name = name
print 'calling from ', self.name
return x**2 + y**2
self.add = types.MethodType(squared_sum, self)
在类定义之外打补丁:
math = Math()
def func(self, x, y, name):
return x ** y
math.add = types.MethodType(func, math)
关于Python 猴子修补对象未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21049374/