__index__

Dunder MethodPython 2.0+Advanced

Returns an integer for use in slicing and bin()/hex()/oct()

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

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

obj = Example()
print(obj)

__index__ returns an integer for use in slicing and bin()/hex()/oct(). Implementing it lets you customize how Python interacts with your objects.

Try in Playground

Tags

oopmagic-methodprotocolcore