1

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.

Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
  • 4
    Not sure how anyone can answer this. Why would you think int was a subclass of float? – wim Apr 15 '20 at 21:32
  • `int` and `float` have completely different implementations, due to the "unlimitied size" of Python `int`. There's no reason for one to inherit from the other. – Błotosmętek Apr 15 '20 at 21:36
  • @Carcigenicate They are implemented in stdlib [`numbers`](https://docs.python.org/3/library/numbers.html). But those ABCs have virtually nothing to do with built-in types `int` and `float`. – wim Apr 15 '20 at 21:39
  • 3
    I’m voting to close this question because the premise was simply a misconception. @OP: since you mentioned the numeric tower, you are probably just confused by the ABCs, in which expressions like `issubclass(numbers.Integral, numbers.Rational)` and `isinstance(1, numbers.Complex)` are true. – wim Apr 15 '20 at 21:42
  • You all can downvote this if you want. In my defense, I think the question is clearly worded, the comments/answers provide clarity and point out an answer. Why would this not add value to new developers (like me), who may have the same question? – Intrastellar Explorer Apr 15 '20 at 21:49
  • 5
    I have not **downvoted** your question. I did vote to close the question, because I feel it's not useful for future reference. If we have a post for every question which was based on a mistaken premise, then that would make up 99% of the content on here. But, anyway, that's just my own opinion and there would have to 2 others agreeing for the closure to happen. – wim Apr 15 '20 at 21:56
  • I guess I agree. Thanks for clearing me up regardless :) – Intrastellar Explorer Apr 15 '20 at 22:03
  • @IntrastellarExplorer I agree. Your question is very clear. Hopefully you have cleared up your misunderstanding. – Code-Apprentice Apr 15 '20 at 22:33

1 Answers1

1

Because int is not a subclass of float:

>>> import inspect
>>> inspect.getmro(int)
(<class 'int'>, <class 'object'>)
>>> inspect.getmro(float)
(<class 'float'>, <class 'object'>)
jfaccioni
  • 7,099
  • 1
  • 9
  • 25
  • 5
    I don't think that will help OP. It's just restating what they've already discovered in a slightly different way. – wim Apr 15 '20 at 21:40