all Python 3astterm
ast.Interactive
root and base nodes
The root node for mode='single': one interactive input line, as typed at the REPL; body is a list of statements.
Syntax
ast.parse(source, mode='single') # → ast.Interactive
The difference from Module is behaviour, not shape: code compiled in 'single' mode prints the value of every expression statement, just like the REPL — exec(compile('1 + 2', '<in>', 'single')) prints 3, while 'exec' mode stays silent. The mode accepts one logical line or one compound statement; two separate lines are a SyntaxError.
Examples
import ast
tree = ast.parse('x = 1; y = 2', mode='single')
print(type(tree).__name__, len(tree.body)) # → Interactive 2
exec(compile(ast.parse('1 + 2', mode='single'), '<in>', 'single'))
# → 3
import ast
try:
ast.parse('x = 1\ny = 2', mode='single')
except SyntaxError as e:
print(type(e).__name__) # → SyntaxError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy