Special Method Protocols
__class_getitem__() — class creation protocol
Handle subscription on a class, often for generic type notation. Implicitly a class method. A metaclass __getitem__ can take precedence. Use established typing mechanisms instead of inventing incompatible generic behavior.
Formal Syntax & Structure
python syntaxpython
def __class_getitem__(cls, item):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| item | object | Subscription argument. |
Return Value
object
Class subscription result.
Working Example
Example Snippetpython
from types import GenericAliasclass Box: def __class_getitem__(cls, item): return GenericAlias(cls, item)print(Box[int].__args__[0].__name__)