Python Glossary Русский

all Python 3astterm

ast.expr

root and base nodes

The abstract base class of all expressions: names, constants, calls, operations, lists. Everything that evaluates to a value is an expr subclass.

Syntax

isinstance(node, ast.expr)

Do not confuse expr with the Expr node: Expr is a statement (stmt) wrapping an expression that stands on its own line, while expr is the expression itself. Expressions carry lineno/col_offset positions; col_offset counts UTF-8 bytes, so after non-ASCII text on the line it differs from the character index.

Examples

import ast
stmt = ast.parse('f(1)').body[0]
print(type(stmt).__name__, isinstance(stmt, ast.expr), isinstance(stmt, ast.stmt))
# → Expr False True
print(type(stmt.value).__name__, isinstance(stmt.value, ast.expr))  # → Call True
import ast
node = ast.parse("'я' + x", mode='eval').body.right
print(node.col_offset)  # → 7 — байты UTF-8, а не символы (их 6)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy