Search code examples
pythoncommentsabstract-syntax-tree

Python AST with preserved comments


I can get AST without comments using

import ast
module = ast.parse(open('/path/to/module.py').read())

Could you show an example of getting AST with preserved comments (and whitespace)?


Solution

  • The ast module doesn't include comments. The tokenize module can give you comments, but doesn't provide other program structure.