Python Glossary Русский

all Python 3astterm

ast.Del

variables

The deletion context: it sits in the ctx field of Name, Attribute and Subscript nodes that are targets of a del statement. Its siblings are Load (read) and Store (write).

Syntax

ast.Name(id, ctx=ast.Del())

Del is not the del statement itself but a marker on its targets; the statement is ast.Delete. In del a[0] only the Subscript gets the Del context, while the name a inside it is read and keeps Load. When you build a tree by hand for compile(), the context has to match the node's role, or the compiler rejects it with ValueError.

Examples

import ast
target = ast.parse('del a[0]').body[0].targets[0]
print(type(target).__name__, type(target.ctx).__name__)  # → Subscript Del
print(type(target.value.ctx).__name__)  # → Load

See also

Open in the full glossarydocs.python.orgReport an inaccuracy