On this page
  1. ast.get_source_segment
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from ast import get_source_segment
help(get_source_segment)

Linked from

Related notes