all Python 3until 3.14Removed from the standard libraryterm
ast.Num / ast.Str / ast.Bytes / ast.NameConstant / ast.Ellipsis
removed
Legacy ast node classes for numbers, strings, bytes, None/True/False and the ellipsis. Removed in Python 3.14 — use ast.Constant.
Syntax
isinstance(node, ast.Num) # до 3.14
Before Python 3.8 the parser produced a separate node for each kind of literal: Num with an n field, Str and Bytes with an s field, NameConstant and Ellipsis. Since 3.8 it emits a single ast.Constant node, and the old names survived as compatibility shims, now removed together with the n and s fields. To migrate, replace isinstance(node, ast.Num) with isinstance(node, ast.Constant) plus a check on the value type, e.g. isinstance(node.value, int); node.n and node.s become node.value; the visit_Num and visit_Str methods of a NodeVisitor become one visit_Constant; ast.Num(n=1) in tree building becomes ast.Constant(value=1).
Examples
# Python 3.14+
import ast
try:
ast.Num
except AttributeError as e:
print(type(e).__name__) # → AttributeError
# Python 3.14+
import ast
class Literals(ast.NodeVisitor):
def visit_Constant(self, node):
print(type(node.value).__name__, repr(node.value))
Literals().visit(ast.parse("x = 42 + len('hi')"))
# → int 42
# → str 'hi'
import ast
node = ast.parse('42').body[0].value
print(isinstance(node, ast.Num), node.n) # → True 42
print(type(node).__name__) # → Constant
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy