pass same method to multiple decorator functions
This question already has an answer here:
Simplest:
@event.listens_for(MyClass.collection, 'append')
@event.listens_for(MyClass.collection, 'remove')
@event.listens_for(MyClass.collection, 'set')
def update_count(target, value, initiator):
target.count = len(target.collection)
ie, just put the decorators one after the other before def
.
Of course, this may cause overhead, eg if each decorator wraps the decorated function into another -- but if you can't fix the decorator, then you can't avoid that overhead. If you can change the decorator, then a listen_multi
variant thereof, that takes all events being listened for in a single gulp, might no doubt offer much better performance.
下一篇: 将相同的方法传递给多个装饰器函数