all Python 3astterm
ast.Mod
operators
The marker for the binary operator % in an ast tree: it sits in BinOp.op and AugAssign.op and has no fields.
Syntax
ast.Mod()
The tree does not tell the remainder from old-style string formatting: '10 % 3' and "'%s' % name" are identical BinOp(Mod) nodes. To find %-formatting, check that left is a Constant holding a str. A % inside an f-string or a literal creates no node.
Examples
import ast
for src in ['10 % 3', "'%s!' % name"]:
b = ast.parse(src, mode='eval').body
print(type(b.op).__name__, isinstance(b.left.value, str) if isinstance(b.left, ast.Constant) else '-')
# → Mod False / Mod True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy