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

The `==` operator is Python's value equality operator. It evaluates whether the internal data or state of two objects is equivalent, regardless of whether they occupy the same memory address.

```python theme={"dark"}
operand_a == operand_b
```

## Internal Mechanics

When the `==` operator is invoked, Python translates the expression into a method call to the `__eq__()` dunder (magic) method. In Python's data model, dunder methods are always looked up on the class, not the instance, to bypass instance dictionaries. Therefore, the internal lookup is conceptually `type(operand_a).__eq__(operand_a, operand_b)`.

The evaluation follows a strict dispatch mechanism that accounts for inheritance:

1. **Subclass Priority Rule:** If `operand_b` is an instance of a strict subclass of `operand_a`'s type, Python reverses the standard dispatch order. It will call `type(operand_b).__eq__(operand_b, operand_a)` first. This ensures that subclasses can properly override and dictate the equality behavior of their parent classes.
2. **Primary Dispatch:** If the subclass rule does not apply, Python attempts to call the `__eq__` method of the left operand: `type(operand_a).__eq__(operand_a, operand_b)`.
3. **Fallback Dispatch:** If the first method called returns the built-in `NotImplemented` constant (indicating it does not know how to compare itself to the other type), Python falls back to the other operand's `__eq__` method.
4. **Identity Fallback:** If both operands return `NotImplemented`, Python falls back to comparing object identity, effectively evaluating `operand_a is operand_b` (comparing their memory addresses).

```python theme={"dark"}

# The conceptual equivalent of the == operator's internal logic
def evaluate_equality(a, b):
    type_a = type(a)
    type_b = type(b)
    
    # 1. Subclass Priority Rule
    if type_a is not type_b and issubclass(type_b, type_a):
        result = type_b.__eq__(b, a)
        if result is not NotImplemented:
            return result
        result = type_a.__eq__(a, b)
        
    # 2. Standard Dispatch
    else:
        result = type_a.__eq__(a, b)
        if result is NotImplemented:
            # 3. Fallback Dispatch
            result = type_b.__eq__(b, a)
            
    # 4. Identity Fallback
    if result is NotImplemented:
        return a is b
        
    return result
```

## Type Handling and Coercion

Python is strongly typed, meaning `==` does not perform implicit type coercion across disparate data types (e.g., a string `"1"` will never equal an integer `1`). However, the operator implements specific behaviors for certain built-in types:

* **Numeric Types:** Python's numeric types (`int`, `float`, `complex`, `decimal`, `fractions`) implement cross-type equality. The `==` operator will evaluate `1 == 1.0` as `True` because their mathematical values are equivalent, despite differing memory representations.
* **Collections:** For built-in container types (like `list`, `tuple`, `dict`, and `set`), the `==` operator performs a deep, recursive equality check. It iterates through the collections, invoking the `==` operator on each corresponding pair of elements. Both the structure and the values must match.
* **Custom Objects:** By default, user-defined classes inherit their `__eq__` method from the base `object` class. The default implementation strictly compares memory addresses. To achieve value equality in custom objects, the developer must explicitly override the `__eq__` method to define the specific attributes that constitute equality.

## Hashing Implications

In Python's data model, the `==` operator is tightly coupled with the `__hash__()` method. If two objects evaluate as equal via the `==` operator (`a == b` is `True`), they must yield the exact same integer value when passed to the `hash()` function, provided they are hashable. If a custom class overrides `__eq__` to provide value equality, Python automatically sets its `__hash__` to `None`, making the object unhashable unless `__hash__` is also explicitly overridden.

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