Python: can a decorator determine if a function is being defined inside a class?

前端 未结 6 759
Happy的楠姐
Happy的楠姐 2021-02-07 11:58

I\'m writing a decorator, and for various annoying reasons[0] it would be expedient to check if the function it is wrapping is being defined stand-alone or as part of a class (a

6条回答
  •  时光说笑
    2021-02-07 12:50

    you can use the package wrapt to check for
    - instance/class methods
    - classes
    - freestanding functions/static methods:

    See the project page of wrapt: https://pypi.org/project/wrapt/

提交回复
热议问题