main
main copied to clipboard
__index__ magic method returns class instance, not integer
__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