Can a method be a decorator of another method of the same class?

前端 未结 1 807
挽巷
挽巷 2020-12-05 13:55

I have a class with a dull repeating pattern on their functions and I wanted to turn this pattern into a decorator. But the thing is that this decorator must access some att

相关标签:
1条回答
  • 2020-12-05 14:37

    The decorator gets only one parameter – the function or method it decorates. It does not get passed an instance as self parameter – at the moment the decorator is called, not even the class has been created, let alone an instance of the class. The instance will be passed as first argument to the decorated function, so you should include self as first parameter in the parameter list of call().

    I don't see the necessity to include the decorator in the class scope. You can do this, but you can just as well have it at module scope.

    0 讨论(0)
提交回复
热议问题