我想对库类的一个方法进行monkey修补,以便为参数定义不同的默认值。失败了:
from functools import partial
class A(object):
def meth(self, foo=1):
print(foo)
A.meth = partial(A.meth, foo=2)
a = A()
a.meth()
用:
Traceback (most recent call last):
File "...", line 10, in <module>
a.meth()
TypeError: meth() missing 1 required positional argument: 'self'
正确的方法是什么?
(原始代码正在使用
getattr
循环中的方法名)
链接的问题中的答案涉及到定义一个新的模块级函数-我想避免一个新的函数定义