> SupportsIndex makes it so that, if you have a value that's not a Python int but can be implicitly and losslessly converted to one
Just to be clear for folks who care more about the general python around this: any class that has a method __index__ that returns an int can be used as an index for array access. e.g:
class FetchIndex:
def __index__(self):
return int(requests.get("https://indextolookup.com").text.strip())
a = ["no", "lol", "wow"]
print(a[FetchIndex()])
The builtin `int` also has a __index__ method that returns itself.
Dunders are part of what makes typing in python so hard.
Just to be clear for folks who care more about the general python around this: any class that has a method __index__ that returns an int can be used as an index for array access. e.g:
The builtin `int` also has a __index__ method that returns itself.Dunders are part of what makes typing in python so hard.