Python Glossary Русский

all Python 3astterm

ast.Raise

statements

Node for the raise statement: exc is what is raised, cause is the expression after from; a bare raise gives exc=None.

Syntax

ast.Raise(exc=None, cause=None)

Raise has two fields, not the three of Python 2 (type, inst, tback). A missing from and from None differ: in the first case cause is None, in the second it is Constant(None), which suppresses the display of the original exception. A bare raise with exc=None re-raises the current exception and fails with RuntimeError outside an except block.

Examples

import ast
a = ast.parse('raise E from None').body[0]
b = ast.parse('raise E').body[0]
print(type(a.cause).__name__, b.cause)  # → Constant None
print(ast.parse('raise').body[0].exc)  # → None

See also

Open in the full glossarydocs.python.orgReport an inaccuracy