I am trying to find a better way to create a class decorator that does the following:
- Inserts several functions into a decorated class
- Makes a call to one of these functions AFTER the decorated class'
__init__ is called
For now, I just keep a reference to the 'original' __init__ method and replace it with my __init__ , which calls the original and my extra function. It looks something like this:
orig_init = cls.__init__ def new_init(self, *args, **kwargs): """ 'Extend' wrapped class' __init__ so we can attach to all signals automatically """ orig_init(self, *args, **kwargs) self._debugSignals() cls.__init__ = new_init
Is there a better way to "increase" the original __init__ or introduce my call to another location? I really need my self._debugSignals() be called some time after creating the object. I also want this to happen automatically, so I thought there was a good place after __init__ .
Extra difference. decorator notes
Perhaps it is worth mentioning some background of this decorator. You can find the full code here . The decorator point should automatically join any PyQt signals and print when they are emitted. The decorator works great when I decorate my QtCore.QObject subclasses, however, I recently tried to automatically decorate all QObject children .
I would like to have a βdebuggingβ mode in the application where I can automatically print ALL signals just to make sure that everything does what I expect. I'm sure this will lead to TONS debugging, but I still would like to see what happens.
The problem is that my current version of the decorator calls segfault when replacing QtCore.QObject.__init__ . I tried to debug this, but the code is all generated by SIP, with which I have little experience.
So, I was wondering if there is a safer, more pythonic way to input a function call AFTER __init__ and hopefully avoid segfault.
durden2.0
source share