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

The `+` operator in Python is a polymorphic operator primarily responsible for numeric addition and sequence concatenation. Its specific behavior is dynamically resolved at runtime based on the data types of the operands being evaluated.

```python theme={"dark"}

# Binary syntax
result = left_operand + right_operand


# Unary syntax
result = +operand
```

## Internal Mechanics and Data Model

When the Python interpreter evaluates a binary `+` expression, it translates the operation into a method call on the underlying objects. To ensure correctness and prevent instances from shadowing special methods, Python looks up dunder methods on the class type, bypassing the instance dictionary entirely. The standard resolution order invokes the left operand's `__add__` special (dunder) method.

```python theme={"dark"}

# Standard internal evaluation of: a + b
result = type(a).__add__(a, b)
```

However, Python enforces a strict subclass priority rule. If the right operand's type is a strict subclass of the left operand's type **and** the subclass overrides the `__radd__` (reverse add) method, the interpreter will bypass the left operand and invoke the right operand's `__radd__` method *before* attempting the left operand's `__add__` method. If the subclass inherits but does not override the `__radd__` method, the interpreter does not bypass the left operand, and standard left-to-right evaluation is preserved.

```python theme={"dark"}

# Evaluation if type(b) is a strict subclass of type(a) 

# AND type(b) overrides __radd__
result = type(b).__radd__(b, a)
```

During standard evaluation, if the left operand's `__add__` method does not recognize or support the type of the right operand, it returns the `NotImplemented` singleton. The interpreter then attempts a reverse addition by invoking the `__radd__` method on the right operand's type. If both `__add__` and `__radd__` return `NotImplemented`, or if the methods do not exist, the interpreter raises a `TypeError`.

## Type Coercion and Compatibility

Because Python is strongly typed, the `+` operator enforces strict type compatibility rules:

* **Numeric Types:** Python performs implicit type promotion (coercion) across the numeric tower. If an `int` and a `float` are operated on, the `int` is promoted to a `float` at the C-API level before the addition is computed.
* **Sequence Types:** Operands generally must belong to compatible sequence families. The operator will not implicitly cast types across distinct sequence boundaries (e.g., `list` + `tuple` raises a `TypeError`). However, strict type equality is not universally required; certain differing sequence types can be concatenated if supported by their underlying implementation. For example, a `bytes` object can be added to a `bytearray` (e.g., `bytearray(b"a") + b"b"`), which evaluates to a new `bytearray`.

## Memory Allocation and Mutability

By standard convention, the binary `+` operator evaluates to a newly allocated object in memory and does not mutate either the left or right operand in place. However, because the `+` operator simply delegates to the `__add__` dunder method, Python does not strictly enforce immutability at the language level. A user-defined class can technically implement `__add__` to mutate the object in place, though doing so violates standard Python design principles.

For built-in sequence types, evaluating the `+` operator requires allocating a new contiguous block of memory large enough to hold the combined contents of both operands, followed by copying the values or references from both original sequences. Consequently, using the `+` operator on sequences results in a time and space complexity of $O(N + M)$, where $N$ and $M$ are the lengths of the respective operands.

## Unary Plus

When used as a unary operator (a single operand to the right of the `+`), the interpreter invokes the `__pos__` dunder method on the operand's type.

```python theme={"dark"}

# Internal evaluation of: +a
result = type(a).__pos__(a)
```

While unary `+` leaves standard integers and floats unchanged, it does not guarantee an identical return value or type. For example, booleans are a standard numeric subtype of integers in Python; evaluating `+True` returns the integer `1`, modifying its type. Furthermore, for standard library types like `decimal.Decimal`, unary `+` applies the current thread's arithmetic context (such as precision limits), which can alter the actual value of the operand.

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