Python Glossary Русский

all Python 3astterm

ast.Break

control flow

The node of a break statement, with no fields: it exits the innermost loop. In the tree it is just an item of a body list.

Syntax

ast.Break()

ast.parse('break') succeeds — the 'break outside loop' check is done by the compiler, so SyntaxError appears only in compile(). A tree from parse() therefore does not guarantee runnable code. The node's _fields is empty, but it has lineno and col_offset like any other statement.

Examples

import ast
tree = ast.parse('break')
print(type(tree.body[0]).__name__, ast.Break._fields)  # → Break ()
try:
    compile(tree, '<src>', 'exec')
except SyntaxError as e:
    print(e.msg)  # → 'break' outside loop

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy