元类在这里无济于事;尽管会根据当前对象的类型(例如实例的类)查找特殊方法,__getattribute__
或者__getattr__
在这样做时不对其进行查询(可能是因为它们本身就是特殊方法)。因此,要捕获所有 dunder方法,您必须全部创建它们。
你可以得到所有的一个相当不错的清单 运营商 的特殊方法(__pow__
,__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
然后将其应用于您的班级:
@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__
明确定义,我们仍然会迷上它。