python装饰器 子类继承_Python:强制装饰器继承吗?

I'm working with quite a large OOP code-base, and I'd like to inject some tracing/logging. The easiest way to do this would be to introduce a decorator around certain methods on some base classes, but unfortunately decorators aren't inherited.

I did try something like the following:

def trace(fn):

def wrapper(instance, *args, **kwargs):

result = fn(instance, *args, **kwargs)

# trace logic...

return result

return wrapper

class BaseClass(object):

def __init__(self, ...):

...

self.__call__ = trace(self.__call__) # line added to end of method

...and while the __call__ method is wrapped (which I can see from printing the instance information to the console) the wrapper function isn't executed as expected.

I also looked briefly at using a metaclass based on this answer, but it instantly breaks other parts of the system that use introspection, so I think that's a no-go.

Is there any other way I can force the application of these decorators around the __call__ method of classes that inherit from BaseClass?

解决方案

Why would metaprogramming mess up introspection? Perhaps you are not using it correctly? Try this (assuming Python2.x):

class MyMeta(type):

def __new__(mcl, name, bases, nmspc):

if "__call__" in nmspc:

nmspc["__call__"] = trace(nmspc["__call__"])

return super(MyMeta, mcl).__new__(mcl, name, bases, nmspc)

class BaseClass(object):

__metaclass__ = MyMeta

You can then simply inherit from BaseClass and __call__ will get wrapped automatically.

I'm not sure what kind of introspection would break that. Unless BaseClass actually does not inherit from object but from something that implements its own meta?? But you can deal with that case as well by forcing MyMeta to inherit from that parent's meta (instead of type).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值