Why int is subclass of number.Number?

Viewed 100

Let's see the following code:

>>> int.__mro__
(<class 'int'>, <class 'object'>)

>>> import numbers
>>> issubclass(int, numbers.Number)
True

We can see there is no Number in __mro__, that means int does not inherit from Number, but why int is subclass of the Number?

1 Answers

Number is an abstract base class (ABC). Normal types can be registered as subclasses of the ABC.

See https://docs.python.org/3/library/abc.html#abc.ABCMeta.register :

register(subclass)

Register subclass as a “virtual subclass” of this ABC.

The numbers module contains the line

Integral.register(int)

Integral is a subclass of Rational < Real < Complex < Number.

Therefore int is a virtual subclass of Number.

Related