|

Bit-level OR — combining flags, uniting sets, and merging dicts.

Bitwise operatorPython 1.0+Live demo
Common call
perms = READ | WRITE
Returns
int with all bits from both; set union; merged dict
Replaces
12 | 10 == 14 (0b1100 | 0b1010 = 0b1110)
Watch out
dict merge: the RIGHT side wins duplicate keys
aaLeft operand.type: int | set | dict · required | bbRight operand — wins key clashes in dict merges.type: int | set | dict · required
int | set | dict

Demo

Live evaluation
Try:
Inputs
aintleft operand
bintright operand
Output
12 | 10
14

12 | 10: every bit set in either operand survives — 0b1110 = 14. Combining single-bit flags (1 | 2 = 3) is the bread-and-butter use.

Operands

NameTypeRequiredDescription
aint | set | dictyesLeft operand.
bint | set | dictyesRight operand — wins key clashes in dict merges.

Return value

int | set | dictInts: a bit is set where EITHER operand has it. Sets: union. Dicts (3.9+): merge, right side winning on key clashes.

Common patterns

Building flag sets
OR the individual bits together.
mode = os.O_CREAT | os.O_WRONLY
Dict merge (3.9+)
New dict, right operand wins clashes.
config = defaults | overrides
Set union
All elements of both.
everyone = admins | editors

Examples

1. Bit OR
12 | 10
Returns
14
2. Flags
0b01 | 0b10
Returns
3
3. Set union
{1, 2} | {2, 3}
Returns
{1, 2, 3}
4. Dict merge
{"a": 1} | {"a": 9, "b": 2}
Returns
{'a': 9, 'b': 2}

Pitfalls

1. Dict merge order matters
Duplicate keys take the RIGHT operand’s value.
Overrides lost
config = overrides | defaults
defaults win — backwards
Fix
config = defaults | overrides
overrides win
2. | is not `or`
No truthiness, no short-circuit.
Wrong tool
name = user_input | "default"
TypeError: unsupported operand type(s) for |: 'str' and 'str'
Fix
name = user_input or "default"
the fallback idiom

When to use

Use it
  • Combining bit flags
  • Set union; dict merging (3.9+)
  • Type unions in annotations: int | None (3.10+)
Reach for something else
  • Logical disjunction → or
  • In-place merge of a dict → dict.update

Notes

Complexity
O(bits) ints; O(len) sets/dicts
Return
new value; operands untouched
CPython impl
Objects/longobject.c :: long_or → __or__
Memory
Set/dict results allocate
Thread-safe
Yes — operands are not mutated

FAQ

PEP 604 (3.10+) overloads | on types to build unions — equivalent to Optional[int]. Same symbol, annotation context.

History

3.10
X | Y union syntax in type annotations (PEP 604).
3.9
dict | dict merge added (PEP 584).