我正在寻找有关如何存储在对象内部的对象上调用的方法的pythonic解决方案。
因为在python中,如果我想捕获例如abs()
方法,我将像这样重载该运算符:
Catcher(object):
def __abs__(self):
self.function = abs
c = Catcher()
abs(c) # Now c.function stores 'abs' as it was called on c
如果我想捕获一个具有其他属性的函数,例如
pow()
,我将使用此函数:Catcher(object):
def __pow__(self, value):
self.function = pow
self.value = value
c = Catcher()
c ** 2 # Now c.function stores 'pow', and c.value stores '2'
现在,我正在寻找一种通用的解决方案,以捕获和存储
Catcher
上调用的任何类型的函数,而无需实现所有重载和其他情况。正如您所看到的,我还想存储方法的属性值(如果它们中有多个,则可能在列表中?)。提前致谢!
最佳答案
元类在这里无济于事;尽管在当前对象的类型上查找了特殊方法(因此,实例的类),但是在这样做时未查询__getattribute__
或__getattr__
(可能因为它们本身是特殊方法)。因此,要捕获所有dunder方法,您必须全部创建它们。
您可以通过枚举 __pow__
module获得所有操作符特殊方法(__gt__
,operator
等)的相当不错的列表:
import operator
operator_hooks = [name for name in dir(operator) if name.startswith('__') and name.endswith('__')]
有了该列表,类装饰器可以是:
def instrument_operator_hooks(cls):
def add_hook(name):
operator_func = getattr(operator, name.strip('_'), None)
existing = getattr(cls, name, None)
def op_hook(self, *args, **kw):
print "Hooking into {}".format(name)
self._function = operator_func
self._params = (args, kw)
if existing is not None:
return existing(self, *args, **kw)
raise AttributeError(name)
try:
setattr(cls, name, op_hook)
except (AttributeError, TypeError):
pass # skip __name__ and __doc__ and the like
for hook_name in operator_hooks:
add_hook(hook_name)
return cls
然后将其应用于您的类(class):
@instrument_operator_hooks
class CatchAll(object):
pass
演示:
>>> c = CatchAll()
>>> c ** 2
Hooking into __pow__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 11, in op_hook
AttributeError: __pow__
>>> c._function
<built-in function pow>
>>> c._params
((2,), {})
因此,即使我们的类没有明确定义
__pow__
,我们仍然会迷上它。
关于python - 如何捕获在python中的对象上调用的任何方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372229/