动态地向类添加装饰器 [英] Dynamically add a decorator to class
问题描述
我有一个相当大且涉及广泛的装饰器,用于调试要动态添加的PyQt信号上课。有没有办法动态地向类添加装饰器?
I have a rather large and involved decorator to debug PyQt signals that I want to dynamically add to a class. Is there a way to add a decorator to a class dynamically?
我可能是从错误的角度解决了这个问题,所以这就是我要完成的工作。
I might be approaching this problem from the wrong angle, so here is what I want to accomplish.
- 我有一个装饰器,可以发现/附加所有pyqt信号
- 此装饰器非常适合调试单个类的信号。但是,有时候我想附加到应用程序中的所有信号上。
- 我想将此装饰器动态地附加到我所有有信号的类上。
可能的解决方案/想法
到目前为止,我已经考虑过几种可能的解决方案:
Possible solutions/ideas
I've thought through a few possible solutions so far:
- 继承:如果我所有的类都具有相同的基类(Python内置的
除外),这将很容易。对象
和PyQt内置的QtCore.QObject
)。我想我可以将这个装饰器附加到我的基础班上,一切都会按预期进行。但是,在此特定应用程序中不是这种情况。我也不想将所有类都更改为具有相同的基类。 - 猴子补丁Python
object
或QtCore.QObject
:我不知道这实际上如何工作。但是,从理论上讲,我可以将这些基类之一__ init __
更改为我在装饰器中定义的new_init
吗?这似乎真的很危险而且很骇人,但是也许这是一种好方式? - 元类:我认为元类在这种情况下不起作用,因为我不得不动态地将
__ metaclass __
属性添加到我想将装饰器注入的类中。我认为这是不可能的,因为插入该属性必须已经构造了类。因此,我定义的任何元类都不会被调用。这是真的?
- Inheritance: This would be easy if all my classes had the same base class (other than Python's built-in
object
and PyQt's built-inQtCore.QObject
). I suppose I could just attach this decorator to my base class and everything would workout as expected. However, this is not the case in this particular application. I don't want to change all my classes to have the same base class either. - Monkey-patch Python
object
orQtCore.QObject
: I don't know how this would work practically. However, in theory could I change one of these base classes'__init__
to be thenew_init
I define in my decorator? This seems really dangerous and hackish but maybe it's a good way? - Metaclasses: I don't think metaclasses will work in this scenario because I'd have to dynamically add the
__metaclass__
attribute to the classes I want to inject the decorator into. I think this is impossible because to insert this attribute the class must have already been constructed. Thus, whatever metaclass I define won't be called. Is this true?
我尝试了几种元类魔术的变体,但似乎没有任何效果。我觉得使用元类可能是完成我想要的事情的一种方法,但是我似乎无法使其正常工作。
I tried a few variants of metaclass magic but nothing seemed to work. I feel like using metaclasses might be a way to accomplish what I want, but I can't seem to get it working.
再次,我可能会考虑所有这些错误。本质上,我想将上面引用的装饰器中的行为附加到应用程序中的所有类(甚至可能是选择类的列表)。另外,如有必要,我可以重构我的装饰器。我不太在乎是否将此行为附加到装饰器或其他机制上。我只是假设此装饰器已经完成了我希望为单个类完成的工作,因此可能很容易扩展。
Again, I might be going about this all wrong. Essentially I want to attach the behavior in my decorator referenced above to all classes in my application (maybe even a list of select classes). Also, I could refactor my decorator if necessary. I don't really care if I attach this behavior with a decorator or another mechanism. I just assumed this decorator already accomplishes what I want for a single class so maybe it was easy to extend.
推荐答案
装饰器无济于事超过自动应用的可调用项。要手动应用它,请用装饰器的返回值替换该类:
Decorators are nothing more than callables that are applied automatically. To apply it manually, replace the class with the return value of the decorator:
import somemodule
somemodule.someclass = debug_signals(somemodule.someclass)
这将替换 somemodule。 someclass
名称,返回值为 debug_signals
,我们通过了原始的 somemodule.someclass
课。
This replaces the somemodule.someclass
name with the return value of debug_signals
, which we passed the original somemodule.someclass
class.
这篇关于动态地向类添加装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!