On this page
typing.SupportsIndex
Prev: typing.SupportsFloat | Up: typing | Next: typing.SupportsInt
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.SupportsIndex
Tags: #public #python3 #stdlib #typing
Import
import typing
from typing import SupportsIndex
Signature & Overview
typing.SupportsIndex(*args, **kwargs)
A protocol with one abstract method index.
Usage
from typing import SupportsIndex
class Idx:
def __index__(self) -> int: return 1
items = ["a", "b", "c"]
assert isinstance(Idx(), SupportsIndex) and items[Idx()] == "b"
Gotchas
- Lossless integer conversion (
__index__): Matchesint,bool,IntEnum, and NumPy integers, while excludingfloat.
REPL Inspect
from typing import SupportsIndex
help(SupportsIndex)
Linked from
Related notes
- tuts/python3/stdlib_module/typing/SupportsFloat Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/SupportsInt Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/typing/AbstractSet Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Annotated Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Any Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_never Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_type shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncContextManager shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncGenerator shared tags: #stdlib, #typing