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

The `[]` operator, formally known as the subscription operator, is a syntactic construct used to access, modify, or delete elements within container objects such as sequences and mappings. It acts as syntactic sugar for the Python data model protocols, delegating operations to the underlying `__getitem__`, `__setitem__`, and `__delitem__` magic (dunder) methods.

```python theme={"dark"}

# Read operation (Subscription)
value = container[key]


# Write operation (Assignment)
container[key] = value


# Delete operation
del container[key]
```

## Protocol Delegation

When the Python interpreter encounters the `[]` operator, it translates the syntax into method calls on the object's class. The behavior of the operator is entirely dependent on how the target object implements these methods:

* **`__getitem__(self, key)`**: Invoked during evaluation (`val = obj[key]`).
* **`__setitem__(self, key, value)`**: Invoked during assignment (`obj[key] = val`).
* **`__delitem__(self, key)`**: Invoked during deletion (`del obj[key]`).

If an object does not implement `__getitem__`, attempting to use the `[]` operator will raise a `TypeError` stating the object is not subscriptable.

## Argument Resolution

The expression evaluated inside the brackets is passed as the `key` argument to the underlying dunder methods. Python handles different key syntaxes by passing specific object types:

**1. Standard Keys**
For single values, the exact object inside the brackets is passed. Sequences (like `list` or `tuple`) expect integers, while mappings (like `dict`) expect hashable objects (objects implementing `__hash__` and `__eq__`).

```python theme={"dark"}
obj[0]       # Calls type(obj).__getitem__(obj, 0)
obj["name"]  # Calls type(obj).__getitem__(obj, "name")
```

**2. Slices**
When the colon (`:`) syntax is used, Python instantiates a built-in `slice` object and passes it as the key. The target object's `__getitem__` method is responsible for handling the `slice` instance.

```python theme={"dark"}
obj[start:stop:step] 

# Translates to:

# type(obj).__getitem__(obj, slice(start, stop, step))
```

**3. Implicit Tuples**
If comma-separated values are provided inside the brackets, Python implicitly packs them into a `tuple`. This is heavily utilized by multidimensional array libraries like NumPy.

```python theme={"dark"}
obj[1, 2, 3] 

# Translates to:

# type(obj).__getitem__(obj, (1, 2, 3))
```

## Exception Contracts

By convention, the Python data model dictates specific exceptions that should be raised when the `[]` operator fails:

* **`IndexError`**: Raised by sequences when an integer key is out of the valid range.
* **`KeyError`**: Raised by mappings when a key does not exist in the underlying hash table.
* **`TypeError`**: Raised when the key is of an inappropriate type (e.g., passing an unhashable type like a `list` to a dictionary, or passing a string index to a list).

## Type Hinting (`__class_getitem__`)

In modern Python (PEP 585), the `[]` operator is also overloaded at the class level to support generic type hinting. When applied to a class rather than an instance, the operator invokes the `__class_getitem__` class method.

```python theme={"dark"}

# Type hinting subscription
Vector = list[float]


# Translates to:

# list.__class_getitem__(float)
```

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