__class_getitem__

Dunder MethodPython 2.0+Expert

Called for subscripting a class (e.g. list[int]); used in generics

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
class Example:
    def __class_getitem__(self):
        return "Example __class_getitem__"

obj = Example()
print(obj)

__class_getitem__ called for subscripting a class (e.g. list[int]); used in generics. Implementing it lets you customize how Python interacts with your objects.

Try in Playground

Tags

oopmagic-methodprotocolcore