PyDocs

Nav

Python Language Services

dump

ast

function (source)
  • dump(node, annotate_fields=True, include_attributes=False, *, color=False, indent=None, show_empty=False)
Return a formatted dump of the tree in node.  This is mainly useful for
debugging purposes.

If annotate_fields is true (by default), the returned string will show the
names and the values for fields. If annotate_fields is false, the result
string will be more compact by omitting unambiguous field names.

Attributes such as line numbers and column offsets are not dumped by default.
If this is wanted, include_attributes can be set to true.

If color is true, the returned string is syntax highlighted using ANSI
escape sequences. If color is false (the default), colored output is always
disabled.

If indent is a non-negative integer or string, then the tree will be
pretty-printed with that indent level. If indent is None (the default),
the tree is dumped on a single line.

If show_empty is False, then empty lists and fields that are None
will be omitted from the output for better readability.
Changed in 3.9, 3.13, 3.15
  • 3.9: Added the indent option.
  • 3.13: Added the show_empty option.
  • 3.15: Omit optional Load() values by default.
  • 3.15: Added the color parameter.
1 Example
>>> tree = ast.parse('print(None)', '?', 'eval')
>>> print(ast.dump(tree, indent=4))
Expression(
    body=Call(
        func=Name(id='print'),
        args=[
            Constant(value=None)]))
>>> print(ast.dump(tree, indent=4, show_empty=True))
Expression(
    body=Call(
        func=Name(id='print', ctx=Load()),
        args=[
            Constant(value=None)],
        keywords=[]))
from the Python docs
See Also
No see-alsos for ast/dump
0 Notes
No notes for dump