Operators

| — Bitwise OR and union

For integers, sets a result bit whenever either input bit is set; two bool operands produce a bool. Sets use | for union, dictionaries for a new merged dictionary (right values replace duplicate keys), and supported type objects for union types. Dictionary and type unions were introduced in Python 3.9 and 3.10 respectively. It does not short-circuit. Custom dispatch uses __or__ and __ror__.

Formal Syntax & Structure

python syntaxpython
left | right

Parameters & Attributes

NameTypeDescription
leftint, set, dict, type or supporting objectFirst operand; its type participates in operation selection.
rightcompatible operandSecond operand; supported values depend on the operand types.

Return Value

object

Result selected by the operand types and their operation implementations.

Working Example

Example Snippetpython
print(0b0101 | 0b1010)print(sorted({1, 2} | {2, 3}))print({"x": 1} | {"x": 2, "y": 3})print(isinstance("a", int | str))
Python Reference