Python Glossary Русский

all Python 3deprecated in 3.9astterm

ast.slice

expressions

Deprecated: the abstract class of the old indexing nodes Index and ExtSlice. Since 3.9 an index is a plain expression and a slice a:b is a Slice node.

Syntax

node.slice  # вместо ast.Index / ast.ExtSlice

Do not confuse it with the Slice node (capital S): Slice is an expr subclass and since 3.9 lives in Subscript.slice, while ast.slice has nothing to do with it, so isinstance(node.slice, ast.slice) is always False. Pre-3.9 code expected Subscript.slice.value via Index; now take Subscript.slice directly, and a[1, 2] gives a Tuple instead of ExtSlice.

Examples

import ast
sub = ast.parse('a[1:2]', mode='eval').body
print(type(sub.slice).__name__, isinstance(sub.slice, ast.slice))  # → Slice False
print(type(ast.parse('a[0]', mode='eval').body.slice).__name__)  # → Constant
print(type(ast.parse('a[1, 2]', mode='eval').body.slice).__name__)  # → Tuple

See also

Open in the full glossarydocs.python.orgReport an inaccuracy