Is there a Python library that analyzes a source code file so that I can list all classes and method names and possibly print them as a tree?

Furthermore, I need to pass a character argument to it which contains the index of a character in the code. Think of it like the caret position. The library should tell me the parent methods and classes (outer parent classes which got inherited by using import statements can be ignored; so that I can build a parent tree) and the methods next to my caret position (like siblings).

That library must also be able to handle all major programming languages (Python, C, C++, Java, Swift, ...)

Does anyone know of such a library or will I have to build one on my own?

1 Answer 1


Does anyone know of such a library or will I have to build one on my own?

I think you might have to build your own. That said, I could possibly offer some advice, having had similar ideas earlier:

  • What you are thinking about is what most code completion tools do -- analyze the structure of the code to offer completion. So perhaps study how some of the popular code cross-references and code completion tools work (eg: cscope or pyscope, jedi or some such)
  • Look up the Language Server Protocol and consider whether that would fit your purposes better than a library. It seems to work very well for a lot of IDEs. In fact, there appears to be a python implementation for it.
  • This is a non-trivial problem to solve generically for ^all major programming languages^. Try to flesh out your requirements to be more specific.

Wish you the best !


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.