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 [] 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.

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

# Type hinting subscription
Vector = list[float]


# Translates to:

# list.__class_getitem__(float)
Master Python with Deep Grasping Methodology!Learn More