Python Glossary Русский

Python 3.11+astterm

ast.TryStar

control flow

Node for try with except* (Python 3.11), used for exception groups; it has the same fields as Try.

Syntax

ast.TryStar(body, handlers, orelse, finalbody)

The fields match Try, and even the handlers are the same ExceptHandler nodes, so only the node type tells the two apart. A traversal written for Try (visit_Try) never sees except* blocks; it needs a visit_TryStar too. Mixing except and except* in one try is not allowed: ast.parse raises SyntaxError.

Examples

import ast
t = ast.parse('try:\n    f()\nexcept* ValueError:\n    pass').body[0]
print(type(t).__name__, type(t.handlers[0]).__name__)  # → TryStar ExceptHandler
print(isinstance(t, ast.Try))  # → False

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy