all Python 3astterm
ast.Return
functions and classes
Node for the return statement: value is the returned expression, or None for a bare return.
Syntax
ast.Return(value=None)
A bare return and return None differ in the tree: in the first value is None, in the second it is Constant(None). return a, b puts a single Tuple into value. ast.parse accepts return outside a function, and only compile() reports "'return' outside function", because parsing and context checks are separate.
Examples
import ast
print(ast.parse('return').body[0].value) # → None
print(type(ast.parse('return None').body[0].value).__name__) # → Constant
print(type(ast.parse('return a, b').body[0].value).__name__) # → Tuple
import ast
try:
compile(ast.parse('return 1'), '<ast>', 'exec')
except SyntaxError as e:
print(e.msg) # → 'return' outside function
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy