main icon indicating copy to clipboard operation
main copied to clipboard

__index__ magic method returns class instance, not integer

Open nukethebees opened this issue 11 years ago • 0 comments

__index__ is meant to return an int/long that can be used by functions like bin(). When used under IronPython it seems to return the object itself.

I've written the following script to highlight the issue.

class IndexTest(object):
    def __init__(self):
        self.value = 10
    def __index__(self):
        return self.value

my_instance = IndexTest()
print(bin(my_instance))

This is the CPython 2.7.9 output.

0b1010

This is the IronPython 2.7.5 output.

Traceback (most recent call last):
  File ".\index_test.py", line 8, in <module>
TypeError: expected int, got IndexTest

nukethebees avatar Mar 13 '15 15:03 nukethebees