python - Decorator that can take both init args and call args? -


Is it possible to create a decorator which is __init __ 'D' followed by other arguments For the methods called?

For example: Import from

  MyDecorator bar = MyDecorator (debug = true) @ bar.myfunc (a = 100) Def Spam (): pass @ bar .myotherfunc (x = false) DIF Egg (): Pass  

If it is is possible, you can work as an example

< / Div>

Of course, a decorator is just a function that accepts the function and returns the function. There is no reason that the function can be an example method (or if you have any arguments, can not be reverted back) it is actually a trivial example (because I'm not sure what you will try to do with it):

MyDecorator (object): def __init __ (self, debug): itself .debug = debug def myfunc (auto, A): DEF decorator (FN): def new_fn (): if Self.debug: print a fn () return new_fn return decorator def myotherfunc (self, x): def decorator (fn): def new_fn (): if self.debug: print x fn () return new_fn returns d Koretr I said like

, I can not think of a use case above my head. But I'm sure they are there.


Comments