On this page
ast.increment_lineno
Prev: ast.get_source_segment | Up: ast | Next: ast.iter_child_nodes
Rel: ast.Add, ast.alias, ast.And, ast.AnnAssign
Ref: docs.python.org — ast.increment_lineno
Tags: #public #python3 #stdlib #ast
Import
import ast
from ast import increment_lineno
Signature & Overview
ast.increment_lineno(node, n=1)
Increment the line number and end line number of each node in the tree starting at node by n. This is useful to "move code" to a different location in a file.
Usage
import ast
tree = ast.parse("x = 1")
ast.increment_lineno(tree, n=10)
assert tree.body[0].lineno == 11
Gotchas
- Handy for template engines / embedded code blocks so tracebacks report the line number in the outer file.
REPL Inspect
from ast import increment_lineno
help(increment_lineno)
Linked from
Related notes
- tuts/python3/stdlib_module/ast/get_source_segment Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/iter_child_nodes 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