Search code examples
pythondecorator

Inspecting Python operation: how to get all decorators literally?


I'm inspecting code in runtime. I want to get all decorators a function/method is decorated with. For example, if it is @classmethod I want to get just the string: "@classmethod", if it is something like @path(MyClass.myop) I want to get the string "@path(MyClass.myop)". I know, I can try to analyze the source code but it is tricky and I'm sure all this information must be stored somewhere and should be retrievable. How can it be achieved?


Solution

  • Thanks to commenters I realized that the task was hopeless, and in the meantime, I realized that this type of processing should be performed using ast module, where everything but comments may be tracked and extracted relatively easily.

    So my answer to may question is: use ast instead.