Python Glossary Русский

all Python 3astterm

ast.BitOr

operators

The class of the bitwise operator |: a BitOr() instance sits in the op of a BinOp or an AugAssign. It also expresses the union of sets, dicts and types such as int | None.

Syntax

ast.BitOr()  # |

The annotation int | None is an ordinary BinOp with BitOr in the tree; there is no special node for type unions. The | in a case 1 | 2 pattern is something else entirely: it is MatchOr, and no BinOp appears there. The logical or is Or in a BoolOp, not BitOr.

Examples

import ast
fn = ast.parse('def f(x: int | None): pass').body[0]
print(type(fn.args.args[0].annotation.op).__name__)  # → BitOr
match = ast.parse('match v:\n    case 1 | 2: pass').body[0]
print(type(match.cases[0].pattern).__name__)  # → MatchOr

See also

Open in the full glossarydocs.python.orgReport an inaccuracy