2

While Python does not officially support docstrings for global variables, it is common to add them anyway:

debug = False
'''Set debug to True to see lots of intermediate results'''

Docstring for variable explains that Python does not directly offer access, but Sphinx is able to pull these out of a module. For something I am working on, I would like to access them as well. I think the ast module can do this. Anyone have any example code (using ast or other standard libraries) that can provide docstrings for variables defined in a module?

Community
  • 1
  • 1
bht
  • 801
  • 8
  • 18
  • 3
    possible duplicate of [Docstring for variable](http://stackoverflow.com/questions/8820276/docstring-for-variable) – ILostMySpoon Jul 31 '15 at 03:16

1 Answers1

1

The astor library parses the entire AST and will reconstitute Python. The version on github is well ahead of the Pypi version, so you should clone it from there.

The tree processing is fairly easy to follow -- you should be able to modify it to only give you the docstrings.

Patrick Maupin
  • 8,024
  • 2
  • 23
  • 42