On this page
ast.get_source_segment
Prev: ast.get_docstring | Up: ast | Next: ast.increment_lineno
Rel: ast.Add, ast.alias, ast.And, ast.AnnAssign
Ref: docs.python.org — ast.get_source_segment
Tags: #public #python3 #stdlib #ast
Import
import ast
from ast import get_source_segment
Signature & Overview
ast.get_source_segment(source, node, *, padded=False)
Get source code segment of the source that generated node.
Usage
import ast
src = "total = (1 + 2) * 3"
tree = ast.parse(src)
expr = tree.body[0].value
assert ast.get_source_segment(src, expr) == "(1 + 2) * 3"
Gotchas
- Requires the original source string: Returns
Noneif the node lacksend_lineno/end_col_offset.
REPL Inspect
from ast import get_source_segment
help(get_source_segment)
Linked from
Related notes
- tuts/python3/stdlib_module/ast/get_docstring Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/increment_lineno Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/Add Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/alias Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/And Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/AnnAssign Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/AST Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ast/arg shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/arguments shared tags: #ast, #stdlib