Python Glossary Русский

all Python 3astterm

ast.IsNot

operators

The marker for the comparison is not in an ast tree: it sits in Compare.ops and has no fields.

Syntax

ast.IsNot()

Two words, one operator: 'a is not b' gives a Compare with IsNot, while 'not a is b' gives UnaryOp(Not) over a Compare with Is; the result is the same, but a lint rule looking for IsNot misses the second spelling. It differs from 'a is (not b)', where the right side is a UnaryOp. Compare marker nodes by type (isinstance), not with ==: ast nodes have no __eq__, so ast.IsNot() == ast.IsNot() is False.

Examples

import ast
print(type(ast.parse('a is not b', mode='eval').body.ops[0]).__name__)  # → IsNot
c = ast.parse('a is (not b)', mode='eval').body
print(type(c.ops[0]).__name__, type(c.comparators[0]).__name__)  # → Is UnaryOp

See also

Open in the full glossarydocs.python.orgReport an inaccuracy