> ## 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.

# Python Bitwise OR

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`.

```python theme={"dark"}
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.

```python theme={"dark"}
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.

```python theme={"dark"}
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.

```python theme={"dark"}

# 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__`.

```python theme={"dark"}
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)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
