Python Glossary Русский

all Python 3astterm

ast.BitXor

operators

The class of the exclusive-or operator ^: a BitXor() instance sits in the op of a BinOp or an AugAssign. For sets it is the symmetric difference.

Syntax

ast.BitXor()  # ^

A common beginner mistake is writing 2 ^ 3 to mean a power: in the tree it is BitXor and the result is 1, not 8. Power is the ** operator and the Pow class. A linter hunting for this typo checks exactly for a BinOp with BitXor and numeric Constant nodes on both sides.

Examples

import ast
node = ast.parse('2 ^ 3', mode='eval').body
print(type(node.op).__name__, eval(compile(ast.Expression(node), '<x>', 'eval')))  # → BitXor 1
print(type(ast.parse('2 ** 3', mode='eval').body.op).__name__)  # → Pow

See also

Open in the full glossarydocs.python.orgReport an inaccuracy