Python Glossary Русский

all Python 3deprecated in 3.9astterm

ast.Index

expressions

Deprecated since 3.9: the old index wrapper in Subscript. The index is now stored as the expression itself, and ast.Index(value=x) returns x.

Syntax

ast.Index(value)  # возвращает value

Since Python 3.9 the slice field of d[1] holds the Constant directly, and d[1:2, 3] holds a Tuple with a Slice inside; the parser no longer creates Index or ExtSlice wrappers. The class remains for old code: ast.Index(value=node) hands back that same node, so isinstance(x, ast.Index) is always false on a parsed tree. Code that unwrapped .value from an Index must read slice directly on 3.9+.

Examples

import ast
sub = ast.parse('d[1]', mode='eval').body
print(type(sub).__name__, type(sub.slice).__name__)  # → Subscript Constant
node = ast.Constant(5)
print(ast.Index(value=node) is node)  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy