Python Glossary Русский

all Python 3astterm

ast.Div

operators

The class of the true division operator /: a Div() instance sits in the op of a BinOp or an AugAssign (a /= b). Floor division // is a separate class, FloorDiv.

Syntax

ast.Div()  # /

The class is named Div even though the operator module calls the function truediv: in Python 2 / did integer division for ints, and the node kept its old name. A search for divisions that may raise ZeroDivisionError must look at Div, FloorDiv and Mod — all three fail on zero. The node has no fields.

Examples

import ast
for src in ['a / b', 'a // b', 'a /= b']:
    tree = ast.parse(src).body[0]
    node = tree.value if isinstance(tree, ast.Expr) else tree
    print(type(node.op).__name__)
# → Div / FloorDiv / Div

See also

Open in the full glossarydocs.python.orgReport an inaccuracy