Python Glossary Русский

all Python 3astterm

ast.mod

root and base nodes

The abstract base class of root nodes: Module, Expression, Interactive and FunctionType. Which root you get is decided by the mode of ast.parse.

Syntax

isinstance(tree, ast.mod)

There are never instances of mod itself: isinstance(tree, ast.mod) simply tells a root apart from any other node. The root depends on the mode: 'exec' → Module, 'eval' → Expression, 'single' → Interactive, 'func_type' → FunctionType. Roots have no lineno; positions are carried by the statements, expressions and some helper nodes inside (arg, keyword, alias, except handlers).

Examples

import ast
for mode in ['exec', 'eval', 'single']:
    tree = ast.parse('1 + 1', mode=mode)
    print(mode, type(tree).__name__, isinstance(tree, ast.mod))
# → exec Module True / eval Expression True / single Interactive True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy