Python: hands-on introspection

I often use third-party libraries - packages and modules that lack documentation. Therefore, learning the source code becomes essential, but it can also be a somewhat tedious task. I (as I suppose all) uses the functions dir()and help()to get started, and I recently started using the module inspect. I would like to know what β€œmethods” you use to dive into poorly documented code and how to increase efficiency in doing so. Many thanks.

+5
source share
2 answers

I find IPython indispensable for this kind of task. The magic commands ?(show docstring) and ??(show source) in combination with the excellent IPython completion system and self-analysis of living objects really change.

Session Example:

In [1]: import sphinx.writers <TAB>
# see available modules and packages - narrow down

In [1]: import shpinx.writers.manpage as manpage
In [2]: manpage.<TAB>
# list and complete on the module contents 

In [3]: manpage.Writer?
# nicely formatted docstring follows

In [4]: manpage.Writer??
# nicely formatted source code follows

In [5]: %edit manpage
# open module in editor
# it really helps if you use something like ctags at this point

In [6]: %edit manpage.Writer
# open module in editor - jump to class Writer

Unfortunately, not all codes can be verified in this way. Think of projects that do things in modules without wrapping them in if __name__ == '__main__'or projects that are highly dependent on magic ( sh comes to mind).

+5
source

I would like to build callgraphs from http://pycallgraph.slowchop.com/ or doxygen.

, AST . , - ( , "func1", , ).

+2

All Articles