Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The | (pipe) operator in Python is a binary operator that natively functions as the bitwise OR operator for integers, but is extensively overloaded within the Python data model to represent union, merging, or alternative operations across various built-in types.

Type-Specific Mechanics

The behavior of the | operator is strictly determined by the types of its operands.

1. Integers (Bitwise OR)

When applied to integers, | performs a bitwise OR operation. It evaluates the binary representation of both operands and returns a new integer where each bit is 1 if the corresponding bit in either operand is 1.
a = 10      # Binary: 1010
b = 6       # Binary: 0110
result = a | b  # Binary: 1110 (Decimal: 14)

2. Sets (Set Union)

When applied to set or frozenset objects, | acts as the union operator. It evaluates both sets and returns a new set containing all unique elements present in the left operand, the right operand, or both.
set_a = {1, 2, 3}
set_b = {3, 4, 5}
result = set_a | set_b  # Evaluates to: {1, 2, 3, 4, 5}

3. Dictionaries (Dictionary Merge)

Introduced in Python 3.9 (PEP 584), applying | to two dict objects performs a merge operation. It returns a new dictionary containing all key-value pairs from both operands. In the event of key collisions, the value from the right operand strictly overwrites the value from the left operand.
dict_a = {'a': 1, 'b': 2}
dict_b = {'b': 99, 'c': 3}
result = dict_a | dict_b  # Evaluates to: {'a': 1, 'b': 99, 'c': 3}

4. Type Hinting (Union Types)

Introduced in Python 3.10 (PEP 604), | operates on type objects to create a types.UnionType. It replaces the legacy typing.Union syntax, indicating that a variable, argument, or return value can accept any of the specified types.

# Evaluates to types.UnionType at runtime
def process(payload: int | str | dict) -> list | None:
    pass

Underlying Data Model Implementation

The | operator is syntactic sugar for specific magic (dunder) methods defined on a class. To support the | operator, a class must implement one or more of the following methods:
  • __or__(self, other): Invoked to evaluate self | other.
  • __ror__(self, other): The reflected (right-side) operand. Invoked to evaluate other | self if the left operand does not implement __or__ or returns NotImplemented.
  • __ior__(self, other): Invoked for the augmented assignment operator (|=). It attempts to perform an in-place mutation of self (e.g., set.update() or dict.update()). If __ior__ is not implemented, Python falls back to self = self | other using __or__.
class CustomType:
    def __init__(self, value):
        self.value = value

    def __or__(self, other):
        if isinstance(other, CustomType):
            return CustomType(self.value + other.value)
        return NotImplemented

obj1 = CustomType(5)
obj2 = CustomType(10)
result = obj1 | obj2  # Triggers obj1.__or__(obj2)
Master Python with Deep Grasping Methodology!Learn More