我正在使用Python 3.5.1。我写了一个库,在某些情况下(当文件直接运行时,即__name__ == '__main__'
),我想装饰其中一个类中的某些方法。它应该装饰所有可以创建的实例。我想以一种非侵入性的方式来执行此操作,即理想情况下,我的库中的类不需要任何特殊的代码。
一段时间后,我设法实现了这样的东西,可以满足我的要求:
def patch(clazz, name, replacement):
def wrap_original(orig):
# when called with the original function, a new function will be returned
# this new function, the wrapper, replaces the original function in the class
# and when called it will call the provided replacement function with the
# original function as first argument and the remaining arguments filled in by Python
def wrapper(*args, **kwargs):
return replacement(orig, *args, **kwargs)
return wrapper
orig = getattr(clazz, name)
setattr(clazz, name, wrap_original(orig))
def replacement_function(orig, self, ... other argumnents ...):
# orig here is the original function, so can be called like this:
# orig(self, ... args ...)
pass
patch(mylib.MyClass, 'method_name', replacemment_function)
令人惊讶的是,尽管我尚未使用类方法对其进行测试,但该代码仍然有效,但现在不需要了。它也修补在修补之前创建的实例,尽管我不确定它是否好; d
上面的代码可以说很困难,我需要花一点时间在写完代码后重新整理一下它的工作方式,以便写出解释性注释。我希望轻松一些。
问题是:Python库中是否有任何东西可以使像这样的代码变得不必要了,而这些代码已经实现了我正在做的事情,但是更好呢?
这里的一位发帖人可悲地删除了她/他的帖子,将我定向到该functools
模块。最后,我决定了以下几点:
def replacement(self, orig, ... other arguments ...):
# orig here is the original function, so can be called like this:
# orig(self, ... args ...)
pass
mylib.MyClass.my_method = functools.partialmethod(replacement, mylib.MyClass.my_method)
切换位置所需的orig
andself
参数,因为partialmethod
将第一个参数绑定到它所在的实例,结果第二个参数将是原始函数(的第二个参数partialmethod
)。看起来更干净。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句