[Python-Dev] int() and math.trunc don't accept objects that only define __index__

Rémi Lapeyre remi.lapeyre at henki.fr
Tue Feb 19 08:55:38 EST 2019

Another point in favor of the change I just noticed is that int()
accept objects defining __index__ as its `base` argument:

    Python 3.7.2 (default, Jan 13 2019, 12:50:01)
    [Clang 10.0.0 (clang-1000.11.45.5)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class MyInt:
    ...     def __index__(self):
    ...             return 4
    >>> int("3", base=MyInt())
    >>> int(MyInt())
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: int() argument must be a string, a bytes-like object or
a number, not 'MyInt'

More information about the Python-Dev mailing list