Search code examples
pythonoopfloating-pointintsubclass

Python why does issubclass(int, float) return False


In Python, why do both issubclass(int, float) and isinstance(1, float) return False?

I always thought that int is a subclass of float.

In [1]: issubclass(int, float)
Out[1]: False

In [2]: isinstance(1, float)
Out[2]: False

Research

https://github.com/Stewori/pytypes/issues/26

Mentions [The numeric tower][1] from PEP 484, but I think that's for type hints.


Solution

  • Because int is not a subclass of float:

    >>> import inspect
    >>> inspect.getmro(int)
    (<class 'int'>, <class 'object'>)
    >>> inspect.getmro(float)
    (<class 'float'>, <class 'object'>)