ast.get_source_segment()
ast.get_source_segment() is a Python function in the AST Module category. Returns the source code segment corresponding to an AST node. The syntax is ast.get_source_segment(source, node, *, padded=False). Returns: Source code string. A typical example: source = "x = 42"
node = ast.parse(source).body[0]
ast.get_source_segment(source, node) # "x = 42". A close sibling is ast.literal_eval(), which safely evaluates a string containing a Python literal (list, dict, tuple, number, string, bool, None). A close sibling is ast.parse(), which parses Python source code into an AST (Abstract Syntax Tree). A close sibling is ast.dump(), which returns a formatted string representation of an AST node. A close sibling is ast.walk(), which recursively yields all descendant AST nodes (depth-first). More about this category: Abstract Syntax Tree — literal_eval, parse, dump, walk. Related AST Module entries: ast.literal_eval(), ast.parse(), ast.dump(), ast.walk(), ast.fix_missing_locations(), ast.copy_location(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.