pass same method to multiple decorator functions

This question already has an answer here:

  • How to make a chain of function decorators? 15 answers

  • 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.

    链接地址: http://www.djcxy.com/p/23820.html

    上一篇: 通过结构作为部署用户激活virtualenv

    下一篇: 将相同的方法传递给多个装饰器函数