On this page
ast.copy_location
Prev: ast.compare | Up: ast | Next: ast.dump
Rel: ast.Add, ast.alias, ast.And, ast.AnnAssign
Ref: docs.python.org — ast.copy_location
Tags: #public #python3 #stdlib #ast
Import
import ast
from ast import copy_location
Signature & Overview
ast.copy_location(new_node, old_node)
Copy source location (lineno, col_offset, end_lineno, and end_col_offset attributes) from old_node to new_node if possible, and return new_node.
Usage
import ast
old = ast.parse("1", mode="eval").body
new = ast.copy_location(ast.Constant(2), old)
assert new.lineno == old.lineno and new.col_offset == old.col_offset
Gotchas
- Call in
NodeTransformerwhen replacing nodes: Keeps line numbers accurate in tracebacks and letscompile()succeed.
REPL Inspect
from ast import copy_location
help(copy_location)
Linked from
Related notes
- tuts/python3/stdlib_module/ast/compare Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/dump 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