import ast source = '1+2-3' root = ast.parse(source) # col_offset should be 0 for both BinOp-s def print_node(node, level=0): print(" " * level, node.__class__.__name__, end="") for att in node._attributes: print(", " + att + "=" + str(getattr(node, att)), end="") print() for child in ast.iter_child_nodes(node): print_node(child, level+1) print_node(root)